#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define pb push_back
#define endl '\n'
#define all(v) v.begin(), v.end()
#define fr(m,n) for(i=m;i<n;i++)
#define frr for(i=n-1;i>=0;i--)
#define ff for(i=0;i<n;i++)
#define ff1 for(i=1;i<=n;i++)
#define frj(m,n) for(j=m;j<n;j++)
#define frl(m,n) for(l=m;l<n;l++)
#define frd(m,n) for(d=m;d<n;d++)
#define wq while(q--)
#define lb lower_bound
#define ub upper_bound
#define yes cout << "YES"
#define no cout << "NO"
#define yesm cout << "Yes"
#define nom cout << "No"
#define inf INT_MAX
#define ext {cout << -1; return;}
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define rall(a) (a).rbegin(),(a).rend()
#define vi vector<int>
#define pi pair<int,int>
#define vvi vector<vi>
#define vc vector<char>
#define vs vector<string>
#define ss set<string>
#define vpi vector<pi>
#define mii map<int,int>
#define mci map<char,int>
#define als(i) cout << i.fi << " " << i.se << endl
#define si set<int>
#define sc set<char>
#define mp make_pair
#define fi first
#define se second
#define sz size()
#define el cout<<endl
bool id = 0, id1 = 0, id2 = 0;
int x = 0, y = 0, z = 0;
int ans = 0, num = 0, sum = 0, mo = 0, me = 0, cnt = 0, mi = inf, ma = 0, mid;
string s, p, st;
int h = 0, w = 0, n = 0, m = 0, t = 0, k = 0, i = 0, j = 0, l = 0, r = 0, q = 0, a = 0, b = 0, c = 0, d = 0;
const int mod = 1e9 + 7;
// const int mod = 998244353;
const double PI = 3.141592653589793238462643383279502884;
const long long N = 2e5 + 2;
// int add(int x, int y) {return ((x + y) % mod + mod) % mod;}
// int mul(int x, int y) {return x * y % mod;}
// long long binpow(long long a, long long b) {a %= mod; long long res = 1; while (b > 0) {if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1;} return res;}
// int inv(int x) {return binpow(x, mod - 2);}
// int divide(int x, int y) {return mul(x, inv(y));}
// ll factorial(ll n) {ll ans = 1; for (int i = 1; i <= n; ++i) {ans = ans * i;} return ans;}
// bool check_prime(ll n) {if (n <= 1)return false; if (n <= 3)return true; if (n % 2 == 0 || n % 3 == 0)return false; for (int i = 5; i * i <= n; i = i + 6)if (n % i == 0 || n % (i + 2) == 0)return false; return true;}
int bintoint(string s) {reverse(all(s)); int xx = 0; int m = 1; frj(0, s.sz) {xx += (s[j] == '1' ? m : 0); m <<= 1;} return xx;}
string inttobin(int k) {string p; while (k != 0) {p += (k & 1) + 48; k >>= 1;} return p;}
// int nCr(int n, int r) {if (n < r) return 0; if (r == 0) return 1; int mm = (fac[r] * fac[n - r]); mm %= mod; k = divide(fac[n], mm); k %= mod; return k;}
vi put(vi ad, int i, int j) {
int n = ad.sz;
vi vw = {ad.begin() + i, ad.begin() + j + 1};
reverse(all(vw));
// vi w = {v.begin(), v.begin() + j + 1};
// vi ww = {v.begin() + j + 1, v.end()};
vi has;
// for (auto i : w)has.pb(i);
for (auto i : vw)has.pb(i);
// for (auto i : ww)has.pb(i);
return has;
}
void solve() {
id = 0, id1 = 0, id2 = 0;
x = 0, y = 0, z = 0, cnt = -1;
ans = 0, num = 0, sum = 0, mo = 0, me = 0, cnt = 0, mi = inf, ma = 0;
s = "", p = "", st = "";
n = 0, m = 0, k = 0, i = 0, j = 0, l = 0, r = 0, q = 0, a = 0, b = 0, c = 0, d = 0;
// cin >> n >> a >> b;
// if (n < a) {
// cout << 0;
// }
// else {
// ans = n - n / a;
// cout << ans;
// }
cin >> n >> s; vi ki, ka;
ff{
if (s[i] == '(') ka.pb(i);
else ki.pb(i);
}
if (ka.sz == ki.sz) {
// for (auto i : ka)cout << i << " "; el;
j = 0;
n = ki.sz;
// k = 2 * n + 1;
ff{
if (ka[i] > ki[i]) ans++;
}
cout << ans*2;
}
else ext;
}
// int fac[N];
// vi pfact[N];
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
int T = 1;
// cin >> T;
// vector <ll> vec;
// bool prime[10005];
// memset(prime, true, sizeof(prime));
// for (ll i = 2; i <= 10000; i++) {
// if (prime[i]) {
// vec.push_back(i);
// for (ll j = i * i; j <= 10000; j += i) {
// prime[j] = false;
// }
// }
// }
// for (int i = 2; i < N; i++)
// {
// if (pfact[i].size() == 0)
// {
// for (int j = i; j < N; j += i)
// pfact[j].pb(i);
// }
// }
// fac[0] = 1;
// fr(1, N) {
// fac[i] = fac[i - 1] * i % mod;
// }
while (T--) {
solve();
cout << endl;
} return 0;
}
1591A - Life of a Flower | 1398C - Good Subarrays |
629A - Far Relative’s Birthday Cake | 1166A - Silent Classroom |
1000B - Light It Up | 218B - Airport |
1463B - Find The Array | 1538C - Number of Pairs |
621B - Wet Shark and Bishops | 476B - Dreamoon and WiFi |
152C - Pocket Book | 1681D - Required Length |
1725D - Deducing Sortability | 1501A - Alexey and Train |
721B - Passwords | 1263D - Secret Passwords |
1371B - Magical Calendar | 1726E - Almost Perfect |
1360C - Similar Pairs | 900A - Find Extra One |
1093D - Beautiful Graph | 748A - Santa Claus and a Place in a Class |
1511B - GCD Length | 676B - Pyramid of Glasses |
597A - Divisibility | 1632A - ABC |
1619D - New Year's Problem | 242B - Big Segment |
938A - Word Correction | 159C - String Manipulation 10 |