#include <bits/stdc++.h>
using namespace std;
const unsigned int M = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    string s;
    cin >> s;
    if (n % 2 != 0) {
      cout << "-1" << endl;
      continue;
    }
    long long int e = 0, f = 0;
    for (long long int i = 0; i < s.size(); i++) {
      if (s[i] == '(')
        e++;
      else
        f++;
    }
    if (e != f) {
      cout << "-1" << endl;
      continue;
    }
    long long int r = 0;
    long long int opn = 0, c = 0;
    for (long long int i = 0; i < s.size(); i++) {
      if (s[i] == '(') {
        opn++;
        continue;
      } else if (s[i] == ')' && opn > 0) {
        opn--;
        continue;
      } else if (s[i] == ')' && opn == 0) {
        c++;
        long long int j = i + 1;
        while (c != opn) {
          if (s[j] == ')')
            c++;
          else
            opn++;
          j++;
        }
        r += j - i;
        i = j - 1;
        opn = 0;
        c = 0;
      }
    }
    cout << r << endl;
  }
  return 0;
}
