#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t = 1;
  while (t--) {
    long long int n, sum = 0, ans = 0, ct = 0, m, l, r, k, c;
    cin >> n;
    string s;
    cin >> s;
    long long int e = 0, st = 0;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '(')
        st++;
      else
        e++;
    }
    if (n % 2 == 1 || st != e) {
      cout << -1 << '\n';
      continue;
    }
    st = 0, e = 0;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == ')') {
        e++;
        if (e > st) ans++;
      } else
        st++;
    }
    cout << ans * 2 << '\n';
  }
  return 0;
}
