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