#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, ans;
char s[N];
int sum[N], a[N];
int main() {
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      sum[i] = sum[i - 1] + 1;
    else
      sum[i] = sum[i - 1] - 1;
  }
  for (int i = 1; i <= n; i++) {
    if (!sum[i]) {
      int flag = 0, p = i;
      while (sum[--p])
        if (sum[p] < 0) flag = 1;
      if (flag) ans += i - p;
    }
  }
  if (sum[n])
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
