#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int len, l, r, cnt, ans;
string s;
int main() {
  scanf("%d", &len);
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      l++;
    else if (s[i] == ')')
      r++;
  }
  if (l != r)
    printf("-1\n");
  else {
    cnt = 0;
    ans = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == ')')
        cnt--;
      else if (s[i] == '(')
        cnt++;
      if ((cnt < 0) || (cnt == 0 && s[i] == '(')) ans++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
