#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int u = 0;
  int c = 0;
  for (long long int i = 0; i < n; i++) {
    if (s[i] == ')') {
      c++;
    } else {
      u++;
    }
  }
  if (c != u) {
    cout << "-1" << endl;
    return 0;
  }
  int ans = 0;
  u = 0;
  c = 0;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      u++;
    } else {
      c++;
    }
    if (u == c) {
      v.push_back(i);
    }
  }
  int p = 0;
  int f = 0;
  for (int i = 0; i < v.size(); i++) {
    int flag = 0;
    int k = 0;
    for (int j = p; j < v[i]; j++) {
      if (s[j] == '(')
        k++;
      else
        k--;
      if (k < 0) flag = 1;
    }
    if (flag == 1) {
      f += v[i] - p + 1;
    }
    p = v[i] + 1;
  }
  cout << f << endl;
}
