#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 100;
const long long int INF64 = (1LL << 61) + 100;
const long long int MOD = 1000 * 1000 * 1000 + 7;
long long int bpow(long long int a, long long int b) {
  if (b == 0) return 1;
  if (b == 1) return a % MOD;
  long long int r = bpow(a, b >> 1);
  r = (r * r) % MOD;
  return (r * (((b & 1) ? a : 1) % MOD)) % MOD;
}
long long int multInverse(long long int a) { return bpow(a, MOD - 2); }
long long int modMult(long long int a, long long int b) {
  return (a * b) % MOD;
}
long long int modSub(long long int a, long long int b) {
  a += MOD;
  long long int r = a - b;
  if (r >= MOD) return r - MOD;
  return r;
}
long long int modAdd(long long int a, long long int b) {
  long long int r = a + b;
  if (r >= MOD) return r - MOD;
  return r;
}
int n;
string s;
bool isExists() {
  int cntOp = 0;
  int cntCl = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      cntOp++;
    else
      cntCl++;
  }
  if (cntOp != cntCl) return false;
  return true;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> s;
  s = "#" + s;
  if (!isExists()) {
    cout << "-1\n";
    return 0;
  }
  int bal = 0;
  bool isCorrect = true;
  int prev = 1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      bal++;
    else
      bal--;
    if (bal == 0) {
      if (!isCorrect) ans += i - prev + 1;
      prev = i + 1;
      isCorrect = true;
      continue;
    }
    if (bal < 0) {
      isCorrect = false;
    }
  }
  cout << ans << "\n";
  return 0;
}
