#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long cnt = 0;
  long long ans = 0;
  long long ops = 0;
  long long cls = 0;
  long long p = -1;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '(') {
      cnt++;
      if (cnt == 0) {
        ans += i - p + 1;
        p = -1;
      }
    } else {
      cnt--;
      if (cnt == -1) p = i;
    }
  }
  if (cnt)
    cout << -1;
  else
    cout << ans;
  return 0;
}
