#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    long long n;
    cin >> n;
    string s;
    cin >> s;
    long long op = 0;
    long long c = 1;
    if (s[0] == ')') c = -1;
    long long ans = 0;
    vector<string> vec;
    long long t = 0;
    if (c < 0) t = 1;
    for (long long i = 1; i < n; i++) {
      if (s[i] == '(')
        c++;
      else
        c--;
      if (c < 0) t = 1;
      if (c == 0) {
        if (t) ans += i - op + 1;
        t = 0;
        op = i + 1;
      }
    }
    if (c != 0) {
      cout << -1;
      return 0;
    }
    if (t) ans += n - op;
    cout << ans;
  }
  return 0;
}
