#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class ParenthesesDiv1Medium { public:
int minSwaps(string s, vector <int> L, vector <int> R)
{
set<pair<int,int>> LR;
for(int i=0; i<L.size(); ++i) {
if((R[i]-L[i]+1)%2==1)
return -1;
LR.emplace(L[i], R[i]+1);
}
// event <pos, open>
deque<pair<int,bool>> events;
for(auto lr: LR) {
events.emplace_back(lr.first, true);
events.emplace_back(lr.second, false);
}
sort(events.begin(), events.end());
vector<string> req;
stack<string> stk;
stk.push("");
for(int i=0; i<=s.size(); ++i) {
while(!events.empty() && events[0].first==i) {
if(events[0].second) {
stk.push("");
} else {
if(!stk.top().empty())
req.push_back(stk.top());
stk.pop();
}
events.pop_front();
}
if(i<s.size())
stk.top() += s[i];
}
return solve(req, stk.top());
}
int sig(const string& s)
{
int d = 0;
for(char c: s) d += (c=='(' ? +1 : -1);
return d;
}
// required to make |require| strings correct, |remaining| is the unconstrained supply of chars.
int solve(const vector<string>& require, const string& remaining)
{
//cerr << "[" << remaining << "]" << endl;
int cost = 0;
vector<int> sv;
for(auto r: require)
{
int sr = sig(r)/2;
sv.push_back(sr);
//cerr << ":: [" << r << "] @ " << sr << endl;
int d = 0;
for(char& c: r)
{
if(sr>0 && d>0 && c=='(') c=')', --sr;
else if(sr<0 && c==')') c='(', ++sr;
d += (c=='(' ? +1 : -1);
}
cost += cost_single(r);
}
int ss = accumulate(sv.begin(), sv.end(), 0);
if(ss>0 && ss>count(remaining.begin(), remaining.end(), ')')) return -1;
if(ss<0 && -ss>count(remaining.begin(), remaining.end(), '(')) return -1;
int nega=0, posi=0; for(int z: sv) if(z<0) nega+=-z; else posi+=z;
return cost + max(nega, posi);
}
int cost_single(string r)
{
int cost = 0;
int d = 0;
for(char c: r) {
d += (c=='(' ? +1 : -1);
if(d==-1) ++cost, d=+1;
}
return cost;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, ParenthesesDiv1Medium().minSwaps(s, L, R));}
int main(){
CASE(0)
string s = ")(";
int L_[] = {0,0,0,0};
vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
int R_[] = {1,1,1,1};
vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
int _ = 1;
END
CASE(1)
string s = "(())";
int L_[] = {0,2};
vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
int R_[] = {1,3};
vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
int _ = 1;
END
CASE(2)
string s = "(((())";
int L_[] = {0,2};
vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
int R_[] = {1,3};
vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
int _ = 2;
END
CASE(3)
string s = "(((((((((";
int L_[] = {0,2};
vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
int R_[] = {1,3};
vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
int _ = -1;
END
CASE(4)
string s = "()()()()";
int L_[] = {0,0,0,0,2,2,2,4,4,6};
vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
int R_[] = {1,3,5,7,3,5,7,5,7,7};
vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
int _ = 0;
END
CASE(5)
string s = ")()(()()((())()()()()()()))(()())()()()(";
int L_[] = {3,5,17,25,35};
vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
int R_[] = {12,10,30,30,38};
vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
int _ = 3;
END
/*
CASE(6)
string s = ;
int L_[] = ;
vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
int R_[] = ;
vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
int _ = ;
END
CASE(7)
string s = ;
int L_[] = ;
vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
int R_[] = ;
vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
int _ = ;
END
*/
}
// END CUT HERE