#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int i, j, n, k, c = 0, c1 = 0;
  string s;
  cin >> n >> s;
  if (n % 2 != 0) {
    cout << -1 << endl;
  } else {
    for (i = 0; i < n; i++) {
      if (s[i] == '(') {
        c++;
      } else {
        c--;
      }
      if (c < 0 || (c == 0 && s[i] == '(')) {
        c1++;
      }
    }
    cout << (c ? -1 : c1);
  }
  return 0;
}
