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