#include <bits/stdc++.h>
using namespace std;
char s[1000005];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int s1 = 0, s2 = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      s1++;
    else
      s2++;
  }
  if (s1 != s2) {
    puts("-1");
    return 0;
  }
  s1 = 0, s2 = 0;
  int ans = 0, last;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(') {
      if (s2 > 0) {
        s2--;
        if (s2 == 0) {
          ans += i - last + 1;
        }
      } else {
        s1++;
      }
    } else {
      if (s1 > 0)
        s1--;
      else {
        if (s2 == 0) last = i;
        s2++;
      }
    }
  }
  cout << ans << endl;
}
