#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, cnt = 0, ans = 0, d = 0;
  cin >> n;
  char a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (a[i] == '(')
      cnt++;
    else
      cnt--;
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      if (a[i] == '(') {
        cnt++;
        d++;
      } else {
        d++;
        cnt--;
      }
      if (cnt == 0 && a[i] == '(') {
        ans += d;
        d = 0;
      } else if (cnt == 0 && a[i] == ')') {
        d = 0;
      }
    }
    cout << ans;
  } else
    cout << "-1";
}
