#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 11, mod = 1e9 + 7, maxm = 450 + 11;
int n, sum, t, ans;
int ss = -1;
string s;
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      t++;
    else
      t--;
    if (t < 0) {
      if (ss == -1) ss = i;
    }
    if (t == 0) {
      if (ss != -1) {
        ans += (i - ss + 1);
        ss = -1;
      }
    }
  }
  if (t == 0)
    cout << ans;
  else
    cout << -1;
  return 0;
}
