#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  int l = 0;
  int r = 0;
  int cnto = 0;
  int cntc = 0;
  bool wrong = false;
  for (int i = 0; i < (n); ++i) {
    if (s[i] == '(') {
      cnto++;
    }
    if (s[i] == ')') {
      cntc++;
    }
  }
  if (cnto != cntc) {
    cout << -1 << endl;
    return 0;
  }
  int cnt = 0;
  int ans = 0;
  int t = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      cnt++;
    } else {
      cnt--;
    }
    if (cnt < 0) {
      wrong = true;
    }
    if (cnt == 0) {
      if (wrong) {
        ans += (i - t + 1);
      }
      t = i + 1;
      wrong = false;
    }
  }
  cout << ans << endl;
}
