#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  string str;
  cin >> str;
  long long int close = 0, open = 0, ano = 0, ind, ans = 0;
  for (long long int i = 0; str[i]; i++) {
    if (str[i] == ')')
      close++;
    else
      open++;
    if (close > open) {
      if (ano == 0) {
        ano = 1;
        ind = i;
      }
    } else if (close == open) {
      if (ano == 1) ans += i - ind + 1;
      ano = 0;
    }
  }
  if (close != open)
    cout << "-1\n";
  else {
    cout << ans << "\n";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  solve();
  return 0;
}
