#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
template <typename T>
inline T gcd(T a, T b) {
  return !b ? a : gcd(b, a % b);
}
template <typename T>
inline T q_pow(T a, T x) {
  T ans = 1, tmp = a;
  while (x) {
    if (x & 1) (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    x >>= 1;
  }
  return ans;
}
template <typename T>
inline void re(T &N) {
  int f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f = -1;
  N = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') N = N * 10 + c - '0';
  N *= f;
}
int m, n, t = 1, st, en;
int a[N], b[N];
char s[N];
int main() {
  re(n);
  int ans = 0;
  scanf("%s", s + 1);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      cnt++;
    else
      cnt--;
    if (cnt < 0) {
      int j = i;
      while (j <= n && cnt < 0) {
        cnt += (s[++j] == '(') ? 1 : -1;
      }
      ans += j - i + 1;
      i = j;
    }
  }
  printf("%d\n", cnt ? -1 : ans);
}
