#include <bits/stdc++.h>
using namespace std;
char s[1000005];
stack<int> st;
int main() {
  int n;
  scanf("%d%s", &n, s + 1);
  int a = 0, b = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      a++;
    else
      b++;
  }
  if (a != b)
    printf("-1\n");
  else {
    int ans = 0;
    int p = 0, q = 0;
    bool f = 0;
    for (int i = 1; i <= n; i++) {
      if (s[i] == '(') {
        if (f) {
          p++;
          if (p == q) {
            f = 0;
            ans += 2 * p;
            p = 0, q = 0;
          }
        } else
          st.push(1);
      } else {
        if (f) {
          q++;
          if (p == q) {
            f = 0;
            ans += 2 * p;
            p = 0, q = 0;
          }
        } else {
          if (st.size())
            st.pop();
          else {
            f = 1;
            q++;
          }
        }
      }
    }
    printf("%d\n", ans);
  }
}
