#include <bits/stdc++.h>
using namespace std;
int const N = 1001000;
char s[N];
int main() {
  int n;
  scanf("%d", &n);
  scanf(" %s", s);
  int x = 0;
  for (int i = 0; i < (n); ++i) {
    if (s[i] == '(') ++x;
  }
  if (x * 2 != n) {
    puts("-1");
    return 0;
  }
  int t = 0;
  int beg = -1, ans = 0;
  for (int i = 0; i < (n); ++i) {
    if (s[i] == '(')
      ++t;
    else
      --t;
    if (t < 0 && beg == -1) {
      beg = i;
    }
    if (t == 0 && beg != -1) {
      ans += i - beg + 1;
      beg = -1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
