#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
char a[maxn];
void solve() {
  int n;
  cin >> n;
  scanf("%s", a + 1);
  int ans = 0;
  int cur = 0, st = -1;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '(')
      cur++;
    else if (a[i] == ')')
      cur--;
    if (cur == 0 && st != -1) ans += i - st + 1, st = -1;
    if (cur < 0 && st == -1) st = i;
  }
  if (cur != 0)
    cout << -1;
  else
    cout << ans;
}
int main() {
  solve();
  return 0;
}
