#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
char a[N];
void solve() {
  int n;
  scanf("%d", &n);
  scanf("%s", a + 1);
  int L = 0, R = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '(')
      L++;
    else if (a[i] == ')')
      R++;
  }
  if (L != R) {
    puts("-1");
    return;
  }
  int pos = -1;
  int now = 0;
  int ans = 0;
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '(') {
      if (tot == 0)
        now++;
      else {
        tot++;
        if (tot == 0) {
          ans += i - pos + 1;
          pos = -1;
        }
      }
    } else if (a[i] == ')') {
      if (now == 0) {
        if (pos == -1) pos = i;
        tot--;
      } else
        now--;
    }
  }
  printf("%d\n", ans);
}
int main() {
  solve();
  return 0;
}
