#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e6 + 123, inf = 1e9, mod = 1e9 + 7;
const long long INF = 1e18;
int n, ans, bal;
char second[N];
int main() {
  scanf("%d", &n);
  scanf("%s", second);
  for (int i = 0; i < n; i++) {
    if (second[i] == '(')
      bal++;
    else
      bal--;
    if (bal < 0 || (bal == 0 && second[i] == '(')) ans++;
  }
  if (bal != 0) {
    printf("-1");
    return 0;
  }
  printf("%d", ans);
}
