#include <bits/stdc++.h>
using namespace std;
int n, m, sa, sb;
char a[2000005];
int x = 0, ans = 0, t;
void rd() {
  cin >> n;
  scanf("%s", a + 1);
}
signed main() {
  rd();
  for (int i = 1; i <= n; i++)
    if (a[i] == '(')
      sa++;
    else
      sb++;
  if (sa != sb) {
    cout << "-1\n";
    return 0;
  }
  t = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '(') x++;
    if (a[i] == ')') x--;
    if (t == 0 && x < 0) t = i;
    if (t > 0 && x >= 0) {
      ans += i - t + 1;
      t = 0;
    }
  }
  cout << ans << "\n";
  return 0;
}
