#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
inline long long ksm(long long a, long long b) {
  long long ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1) ans = ans * a % mod;
  return ans;
}
string s;
long long che(string s) {
  long long c0 = 0, c1 = 0;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      c0++;
    else {
      if (c0)
        c0--;
      else
        c1++;
    }
  }
  return !c0 && !c1;
}
signed main() {
  long long n;
  cin >> n;
  cin >> s;
  long long c0 = 0, c1 = 0;
  long long ans = 0;
  string res;
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '(')
      c0++;
    else
      c1++;
    res += s[i];
    if (c0 == c1) {
      if (che(res) == 0) ans += res.size();
      cnt += res.size();
      res = "";
      c0 = c1 = 0;
    }
  }
  if (cnt != n)
    cout << -1 << endl;
  else
    cout << ans << endl;
  getchar();
  getchar();
}
