#include <bits/stdc++.h>
using namespace std;
inline long long int maxim(long long int a, long long int b) {
  if (a > b)
    return a;
  else
    return b;
}
inline long long int minim(long long int a, long long int b) {
  if (a < b)
    return a;
  else
    return b;
}
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
long long int gcd(long long int a, long long int b) {
  return b == 0 ? a : gcd(b, a % b);
}
long long int binpow(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    int q = a / m;
    int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
bool comp(long long int a, long long int b) { return a > b; }
long long int arr[1000010];
long long int temp[1000010];
set<long long int> f;
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  cout << setprecision(10) << fixed;
  long long int t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    string s;
    cin >> s;
    long long int l = -1;
    long long int r = -1;
    if (s[0] == '(') {
      arr[0] = 1;
    } else {
      arr[0] = -1;
      l = 0;
      r = 0;
    }
    long long int count = 0;
    for (long long int o = (1), _b = (n - 1); o <= _b; o++) {
      if (s[o] == '(') {
        arr[o] = arr[o - 1] + 1;
      } else {
        arr[o] = arr[o - 1] - 1;
      }
      if (arr[o] >= 0) {
        if (l != -1) {
          count += (o - l + 1);
          l = -1;
        }
      } else {
        if (l == -1) {
          l = o;
        }
      }
    }
    if (arr[n - 1] == 0)
      cout << count << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
