#include <bits/stdc++.h>
using namespace std;
void FAST() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int gcd(int x, int y) {
  if (x == 0) return y;
  return gcd(y % x, x);
}
int powM(int x, int y, int m) {
  int ans = 1, r = 1;
  x %= m;
  while (r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}
map<long long, long long> factorize(long long n) {
  map<long long, long long> ans;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ans[i]++;
      n /= i;
    }
  }
  if (n > 1) {
    ans[n]++;
    n = 1;
  }
  return ans;
}
const double pi = 3.14159265;
int main() {
  FAST();
  int n;
  cin >> n;
  string s;
  cin >> s;
  int o = 0, c = 0;
  for (auto x : s) {
    if (x == '(')
      o++;
    else
      c++;
  }
  if (o != c)
    cout << -1;
  else {
    int depth = 0, last = -1, ans = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '(') {
        depth++;
      } else {
        depth--;
      }
      if (depth == 0 && s[i] == '(') {
        ans += (i - last);
        last = i;
      }
      if (depth == 0) last = i;
    }
    cout << ans;
  }
  return 0;
}
