#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
int n, c;
char s[1000005];
int st[1000005], val[1000005], top, res;
vector<pair<int, int> > v;
signed main() {
  n = read();
  scanf("%s", s + 1);
  for (register int i = (1); i <= (n); ++i)
    if (s[i] == '(') c++;
  if (c != n - c) return puts("-1") & 0;
  for (register int i = (1); i <= (n); ++i) {
    if (s[i] == '(') {
      if (val[top] == -1) {
        v.push_back(make_pair(st[top], i));
        top--;
      } else
        st[++top] = i, val[top] = 1;
    } else {
      if (val[top] == 1)
        top--;
      else
        st[++top] = i, val[top] = -1;
    }
  }
  sort(v.begin(), v.end());
  int now = 0;
  for (int i = 0; i < v.size(); ++i) {
    if (v[i].second <= now) continue;
    res += v[i].second - v[i].first + 1;
    now = v[i].second;
  }
  cout << res;
  return 0;
}
