#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int power(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int mod_inv(int a, int m) {
  a = a % m;
  return power(a, m - 2, m) % 10 ^ 9 + 7;
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
bool mod(double a, double b) { return a / b - floor(a / b); }
int logb(int base, int x) { return (log(x) / log(base)); }
int parent[1000000], Size[10000000];
int find_set(int x) {
  if (x == parent[x]) return x;
  return parent[x] = find_set(parent[x]);
}
void Union(int x, int y) {
  x = find_set(x);
  y = find_set(y);
  if (x != y) {
    if (Size[x] > Size[y]) {
      parent[y] = parent[x];
      Size[x] += Size[y];
    } else {
      parent[x] = parent[y];
      Size[y] += Size[x];
    }
  }
}
void make_set(int x) {
  parent[x] = x;
  Size[x] = 1;
}
signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int c = 0, o = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      o++;
    else
      c++;
  }
  if (o != c)
    cout << -1 << endl;
  else {
    bool flag = true;
    int end = 0, c = 0, ans = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '(')
        c++;
      else
        c--;
      if (flag && c < 0) {
        flag = false;
        end = i;
      }
      if (!flag && c == 0) {
        ans += (i - end + 1);
        flag = true;
      }
    }
    cout << ans << endl;
  }
}
