#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const double PI = 3.141592653589793238463;
const int MAXN = 2e5 + 5;
long long fastPow(long long x, long long b) {
  if (b == 0)
    return 1;
  else if (!(b & 1)) {
    return fastPow(x * x, b / 2);
  } else
    return x * fastPow(x * x, (b - 1) / 2);
}
char arr[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string h;
  cin >> h;
  int c1 = 0, c2 = 0;
  for (int i = 0; i < n; i++) {
    if (h[i] == '(')
      c1++;
    else
      c2++;
  }
  if (c1 != c2) return cout << -1, 0;
  int c = 0;
  int ind = -1;
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    if (h[i] == '(') {
      c++;
      if (c == 0) {
        ans += (i - ind) + 1;
      }
    } else {
      c--;
      if (c == -1) ind = i;
    }
  }
  cout << ans << '\n';
  return 0;
}
