#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, ans = 0;
  cin >> n;
  string s;
  cin >> s;
  if (n % 2) {
    cout << -1;
    return;
  }
  int f = count(s.begin(), s.end(), '('), b = count(s.begin(), s.end(), ')');
  if (f != b) {
    cout << -1;
    return;
  }
  bool ok = false;
  int bal = 0, cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!bal && s[i - cnt] == ')') ans += cnt;
    if (!bal) cnt = 0;
    bal += (s[i] == '(' ? 1 : -1);
    cnt++;
  }
  if (!bal && s[n - cnt] == ')') ans += cnt, cnt = 0;
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
