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