#include <bits/stdc++.h>
using namespace std;
char s[1000010];
int a[1000010];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      a[i] = a[i - 1] + 1;
    else
      a[i] = a[i - 1] - 1;
  }
  if (a[n] != 0) {
    printf("-1");
    return 0;
  }
  int len = 0;
  int ans = 0;
  int flag = 0;
  for (int i = 1; i <= n; i++) {
    len++;
    if (a[i] < 0) flag = 1;
    if (a[i] == 0) {
      if (flag) ans += len;
      flag = 0;
      len = 0;
    }
  }
  printf("%d", ans);
}
