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