#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
bool sortbysec(const pair<long long int, long long int> &a,
               const pair<long long int, long long int> &b) {
  return (a.second > b.second);
}
string decToBin(long long int n) {
  string s = "0";
  if (n == 0) return s;
  s = "";
  while (n > 0) {
    s += (n % 2) + '0';
    n = n / 2;
  }
  reverse(s.begin(), s.end());
  return s;
}
long long int binToDec(string s) {
  long long int ans = 0;
  reverse(s.begin(), s.end());
  for (__typeof(s.size()) i = (0) - ((0) > (s.size()));
       i != (s.size()) - ((0) > (s.size())); i += 1 - 2 * ((0) > (s.size()))) {
    ans += pow(2, i) * (s[i] - '0');
  }
  return ans;
}
long long int nCr(long long int n, long long int r) {
  vector<long long int> fac(200000);
  fac.push_back(1);
  for (long long int i = 1; i < n + 1; i++) {
    fac.push_back((fac[i - 1] * i) % 1000000007);
  }
  long long int ans = fac[n] / (fac[n - r] * fac[r]);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  long long int mn = 0;
  vector<long long int> st;
  if (n % 2 == 1) {
    cout << -1 << endl;
    exit(0);
  }
  long long int op = 0, cl = 0;
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    if (s[i] == '(') {
      st.push_back(0);
      op++;
    } else {
      if (st.size() != 0) {
        st.pop_back();
      }
      cl++;
    }
    if (cl == op) {
      if (st.size() == 0) {
        cl = 0;
        op = 0;
        continue;
      }
      mn += cl + op;
      cl = 0;
      op = 0;
      st.clear();
    }
  }
  if (cl == 0 && op == 0)
    cout << mn << endl;
  else
    cout << -1 << endl;
  return 0;
}
