#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
  int i, j, k, l, m, n, x, y, t, lst, val, cnt = 0, ans = 0;
  cin >> n;
  cin >> s;
  lst = 0;
  val = 1;
  for (i = 0; i < n; i++) {
    if (s[i] == '(') {
      cnt++;
    } else {
      cnt--;
    }
    if (cnt < 0) {
      val = 0;
    } else if (cnt == 0 && val == 0) {
      ans += i - lst + 1;
      lst = i + 1;
      val = 1;
    } else if (cnt == 0) {
      val = 1;
      lst = i + 1;
    }
  }
  if (cnt)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
