#include <bits/stdc++.h>
using namespace std;
char s[1000005];
int main() {
  int n;
  cin >> n;
  cin >> s;
  int a = 0, l = strlen(s), cnt = 0;
  for (int i = 0; i < l; i++) {
    if (s[i] == '(')
      a++;
    else {
      a--;
      if (a < 0) cnt++;
    }
  }
  if (a)
    cout << -1 << endl;
  else
    cout << cnt * 2 << endl;
}
