#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void testcase() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int oc = 0, cc = 0;
  for (int i = 0; i <= (n - 1); ++i) {
    if (s[i] == '(')
      oc++;
    else
      cc++;
  }
  if (oc != cc) {
    cout << "-1\n";
    return;
  }
  int unmatched = 0, res = 0, pre = -1;
  for (int i = 0; i <= (n - 1); ++i) {
    if (s[i] == ')') {
      unmatched++;
      if (unmatched == 1) pre = i;
    }
    if (s[i] == '(') unmatched--;
    if (unmatched == 0) {
      if (pre != -1) {
        res += (i - pre + 1);
        pre = -1;
      }
    }
  }
  cout << res << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  if (0) cin >> t;
  while (t--) {
    testcase();
  }
  return 0;
}
