#include <bits/stdc++.h>
const long long int maxc = 10005;
const long long int mod = 1000000000;
using namespace std;
bool valid(long long int x, long long int y, long long int n) {
  return x >= 0 && y >= 0 && x < 2 && y < n;
}
void solve() {
  long long int q, n, m, i, j, x, y, e, t, T, ans, cnt, mn, sum, mx, f, c, k, l,
      r;
  bool flag;
  t = 1;
  while (t--) {
    cnt = 0;
    long long int open = 0, close = 0;
    ans = 0;
    cin >> n;
    string s;
    cin >> s;
    for (long long int i = 0; i < n; i++) s[i] == ')' ? cnt++ : cnt--;
    if (cnt != 0) {
      cout << "-1";
      continue;
    }
    for (long long int i = 0; i < n; i++) {
      if (s[i] == ')') {
        if (open > 0 && close == 0)
          open--;
        else
          close++;
      } else
        open++;
      if (open == close) {
        ans += 2 * open;
        open = close = 0;
      }
    }
    cout << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
