#include <bits/stdc++.h>
using namespace std;
const long long mx = 105;
const long long mx1 = 1000001;
const long long mod = 1000000007;
vector<vector<long long> > edges;
void adds(long long &a, long long b) {
  a += b;
  if (a >= mod) a -= mod;
}
long long mul(long long a, long long b) { return (long long)a * b % mod; }
void max_self(long long &a, long long b) { a = max(a, b); }
int main() {
  int n, i, res = 0, inc1 = -1, lp = 0, rp = 0;
  cin >> n;
  string s;
  cin >> s;
  for (i = 0; i < s.size(); i++) {
    if (s[i] == '(') {
      lp++;
    } else {
      rp++;
    }
    if (rp > lp && inc1 == -1) {
      inc1 = i;
    } else if (lp == rp && inc1 != -1) {
      res += i - inc1 + 1;
      inc1 = -1;
    }
  }
  if (lp != rp)
    cout << "-1\n";
  else {
    cout << res << "\n";
  }
}
