#include <bits/stdc++.h>
using namespace std;
char str[1000001];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str);
  int sum = 0;
  int ln = -1;
  int i, j, k, ans = 0;
  for (i = 0; i < n; i++) {
    if (str[i] == '(') {
      sum++;
      if (sum == 0) {
        ans += i - ln + 1;
        ln = -1;
      }
    } else {
      sum--;
      if (sum < 0 && ln == -1) ln = i;
    }
  }
  if (sum != 0) {
    printf("-1\n");
  } else
    printf("%d\n", ans);
  return 0;
}
