#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sz = s.size(), x = 0, y = 0;
  for (int i = 0; i < sz; i++) {
    if (s[i] == '(') {
      x++;
    } else {
      y++;
    }
  }
  if (x != y) {
    cout << -1 << endl;
    return 0;
  }
  int bal = 0, ans = 0;
  for (int i = 0; i < sz; i++) {
    if (s[i] == '(')
      bal++;
    else
      bal--;
    if (bal < 0) {
      int r = 0;
      for (int j = i + 1; j < n; j++) {
        if (s[j] == '(')
          bal++;
        else
          bal--;
        if (bal == 0) {
          r = j;
          break;
        }
      }
      ans += (r - i + 1);
      i = r;
    }
  }
  cout << ans << endl;
}
