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