#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 998244353;
struct H {
  int p, id;
} he[N];
long long qpow(long long base, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = (ans * base) % mod;
    n >>= 1;
    base = base * base % mod;
  }
  return ans;
}
int n, m, k;
int ar[N];
vector<int> ed[N];
map<int, bool> vis;
int br[N];
long long ans;
char ch[N];
char stk[N];
int tot;
int main() {
  {
    cin >> n;
    scanf("%s", ch + 1);
    int f = 0;
    for (int i = 1; i <= n; i++) {
      if (ch[i] == '(')
        f++;
      else
        f--;
    }
    if (f != 0) {
      printf("-1\n");
      return 0;
    }
    int l = 1;
    for (int i = 1; i <= n; i++) {
      if (ch[i] == '(')
        f++;
      else
        f--;
      if (f == 0) {
        char hd;
        tot = 0;
        for (int j = l; j <= i; j++) {
          if (tot == 0) {
            stk[++tot] = ch[j];
          } else {
            hd = stk[tot];
            if (hd == '(' && ch[j] == ')')
              tot--;
            else
              stk[++tot] = ch[j];
          }
        }
        if (tot != 0) ans += i - l + 1;
        l = i + 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
