#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, p = 0, r = 0;
  cin >> n;
  char a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == '(') {
      if (p < 0 || p + 1 < 0) r++;
      p++;
    } else {
      if (p - 1 < 0 || p < 0) r++;
      p--;
    }
  }
  if (p == 0)
    printf("%d", r);
  else
    printf("-1");
}
