#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  int copen = 0, cclose = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == '(')
      copen++;
    else
      cclose++;
  }
  if (copen != cclose)
    cout << -1 << "\n";
  else {
    int i = 0, c = 0, start = -2, ans = 0;
    while (i < n) {
      if (str[i] == '(')
        c++;
      else
        c--;
      if ((c == -1) && (start == -2)) start = i - 1;
      if ((c == 0) && (start != -2)) {
        ans += (i - start);
        start = -2;
      }
      i++;
    }
    cout << ans << "\n";
  }
}
