#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long ans = 0, k = 0;
  vector<long long> bal;
  for (long long i = 0; i <= n - 1; i += 1) {
    if (s[i] == '(')
      k++;
    else
      k--;
    bal.push_back(k);
  }
  if (k != 0)
    cout << -1 << endl;
  else {
    if (bal[0] < 0) ans++;
    for (long long i = 1; i <= n - 1; i += 1)
      if (bal[i - 1] <= 0 && bal[i] <= 0) ans++;
    cout << ans << endl;
  }
}
bool mtest = false;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int TE = 1;
  if (mtest) cin >> TE;
  while (TE--) solve();
  return 0;
}
