#include <bits/stdc++.h>
using namespace std;
long long int powr(long long a, long long b) {
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 0) {
    return powr(((a % 1000000007) * (a % 1000000007)) % 1000000007, b / 2);
  } else {
    if (b == 1) {
      return a;
    } else {
      return (a * powr(((a % 1000000007) * (a % 1000000007) % 1000000007),
                       (b - 1) / 2)) %
             1000000007;
    }
  }
}
int gcd1(int a, int b) {
  if (a == 0)
    return b;
  else {
    return gcd1(b % a, a);
  }
}
int mod1(int a) {
  if (a < 0)
    return -1 * a;
  else {
    return a;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int r;
  r = 1;
  for (int k = 0; k < r; k++) {
    long long n, count = 0, mount = 0, add = 0, arr[1000000], flag = 0;
    cin >> n;
    string str;
    cin >> str;
    if (n % 2 != 0) {
      cin >> str;
      cout << -1;
    } else {
      for (int i = 0; i < n; i++) {
        arr[i] = 0;
      }
      for (int i = 0; i < n; i++) {
        if (str[i] == ')') {
          count--;
          arr[i] = -1;
        } else {
          count++;
          arr[i] = 1;
        }
      }
      if (count != 0) {
        cout << -1;
      } else {
        count = 0;
        for (int i = 0; i < n; i++) {
          if (arr[i] < 0) {
            count--;
          } else {
            count++;
          }
          if (count == 0 && flag == 1) {
            mount++;
            add += mount;
            mount = 0;
            flag = 0;
          }
          if (count < 0) {
            flag = 1;
            mount++;
          }
        }
        cout << add;
      }
    }
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}
