#include <bits/stdc++.h>
using namespace std;
int n, ans = 0;
string s;
int main() {
  cin >> n >> s;
  int cnt = 0;
  int f = -1;
  for (int i = 0; i < n; i++) {
    if (s[i] == ')')
      cnt--;
    else
      cnt++;
    if (cnt == 0) {
      if (s[i] == '(') ans += i - f;
      f = i;
    }
  }
  if (cnt != 0)
    cout << "-1\n";
  else
    cout << ans << '\n';
  return 0;
}
