#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
int32_t main() {
  long long n, o = 0, c = 0;
  string s;
  cin >> n >> s;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '(')
      o++;
    else
      c++;
  }
  if (o != c) return cout << -1 << endl, false;
  long long ans = 0;
  for (long long i = n - 1; i >= 0; i--) {
    if (s[i] == ')') {
      o = 0;
      c = 1;
      while (o != c) {
        i--;
        if (s[i] == ')')
          c++;
        else
          o++;
      }
    } else {
      o = 1;
      c = 0;
      long long pt = i;
      while (o != c) {
        pt--;
        if (s[pt] == ')')
          c++;
        else
          o++;
      }
      ans += i - pt + 1;
      i = pt;
    }
  }
  cout << ans << endl;
  return false;
}
