#include <bits/stdc++.h>
using namespace std;
int n, ss, ok, ans;
int main() {
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; i++) ss += (s[i] == '(' ? 1 : -1);
  if (ss) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0, lst; i < n; i++) {
    if (s[i] == '(')
      ss++;
    else if ((--ss) == -1)
      ok = 1, lst = i;
    if (!ss && ok) ans += i - lst + 1, ok = 0;
  }
  cout << ans << endl;
  return 0;
}
