#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T& n) {
  char ch;
  int sign = 1;
  while (!isdigit(ch = getchar()))
    if (ch == '-') sign = -1;
  n = ch - '0';
  while (isdigit(ch = getchar())) n = n * 10 + ch - '0';
  n *= sign;
}
const int INF = 1e9 + 7;
const int N = 1222222;
int n, tp;
char s[N];
int stk[N];
int main() {
  read(n);
  scanf("%s", s + 1);
  int s1 = 0, s2 = 0, ans = 0;
  for (int i = (1); i <= (n); ++i) {
    if (s[i] == '(') {
      stk[++tp] = 1;
      s1++;
    } else {
      s2++;
      if (stk[tp] == 1)
        tp--;
      else
        stk[++tp] = 2;
    }
    if (s1 == s2) {
      if (tp != 0) ans += s1 + s2, tp = 0;
      s1 = s2 = 0;
    }
  }
  if (tp != 0)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}
