#include <bits/stdc++.h>
using namespace std;
const long long INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int open = count((s).begin(), (s).end(), '(');
  int close = n - open;
  if (open != close) {
    cout << "-1\n";
    return 0;
  }
  int res = n;
  int cur = 0, op = 0;
  for (int i = 0; i < n; ++i) {
    op += (s[i] == '(' ? +1 : -1);
    if (op < 0) {
      int j;
      for (j = i + 1; j < n and op < 0; ++j) {
        op += (s[j] == '(' ? +1 : -1);
      }
      int len = j - i;
      cur += len;
      i = j - 1;
    }
  }
  res = min(res, cur);
  cout << res << "\n";
  return 0;
}
