#include <bits/stdc++.h>
using namespace std;
int main() {
  stack<char> q;
  int n, flag = 0, t1 = 0, t2 = 0, ans = 0;
  char s[1000010];
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    if (flag == 1 && t1 == t2) {
      ans += t1 * 2;
      flag = 0;
      t1 = 0;
      t2 = 0;
    }
    if (flag == 0) {
      if (s[i] == '(') {
        q.push(s[i]);
      } else {
        if (q.empty()) {
          flag = 1;
          t2++;
        } else if (q.top() == '(') {
          q.pop();
        }
      }
    } else {
      if (s[i] == '(') {
        t1++;
      } else {
        t2++;
      }
    }
  }
  if (flag == 1 && t1 == t2) {
    ans += t1 * 2;
    flag = 0;
    t1 = 0;
    t2 = 0;
  }
  if (flag == 0 && q.empty()) {
    printf("%d\n", ans);
  } else {
    printf("-1\n");
  }
  return 0;
}
