#include <bits/stdc++.h>
using namespace std;
char s[1000100];
int n, l, r;
void init() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  return;
}
void solve() {
  int ans = 0;
  l = r = 0;
  int x = 0, y = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      ++l;
    else
      ++r;
    if (x == 0) {
      if (l < r) x = i;
    } else {
      if (l == r) {
        y = i;
        ans += y - x + 1;
        x = 0;
        y = 0;
      }
    }
  }
  if (l == r)
    printf("%d\n", ans);
  else
    printf("-1\n");
  return;
}
int main() {
  init();
  solve();
  return 0;
}
