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