#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, nl = 0, nr = 0;
  cin >> x;
  char *a;
  a = new char[x + 1];
  for (int i = 1; i <= x; i++) {
    cin >> a[i];
    if (a[i] == '(')
      nl++;
    else
      nr++;
  }
  if (nr != nl)
    cout << -1;
  else {
    int sum = 0, i = 1, ans = 0;
    while (i != x + 2) {
      if (a[i] == '(')
        sum++;
      else
        sum--;
      if (sum < 0 && a[i] == ')') ans += 2;
      i++;
    }
    cout << ans;
  }
  return 0;
}
