#include <bits/stdc++.h>
using namespace std;
const long long pp = 1e9 + 7;
const long double eps = 1e-8;
inline long long ksm(long long x, long long y = pp - 2) {
  x = (x % pp + pp) % pp;
  long long ans = 1;
  while (y) {
    if (y & 1) ans = ans * x % pp;
    x = x * x % pp;
    y >>= 1;
  }
  return ans;
}
inline long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
inline void reads(char s[], int &n) {
  n = 0;
  char ch = getchar();
  while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
  while (ch != ' ' && ch != '\n' && ch != '\r' && ch != EOF)
    s[++n] = ch, ch = getchar();
}
int n, p, c, ans;
char S[1000009];
int main() {
  scanf("%d", &n);
  scanf("%s", S + 1);
  c = 0;
  p = n + 1;
  for (int i = 1; i <= n; i++) {
    if (S[i] == '(')
      c++;
    else
      c--;
    if (c < 0) {
      p = min(p, i);
    }
    if (c >= 0 && p != n + 1) {
      ans += i - p + 1;
      p = n + 1;
    }
  }
  if (c != 0)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}
