#include <bits/stdc++.h>
using namespace std;
bool comp(pair<long long int, long long int> &a,
          pair<long long int, long long int> &b) {
  return (a.second < b.second);
}
long long int power(long long int n, long long int p) {
  if (p == 0)
    return 1;
  else {
    long long int ans = power(n, p / 2) % 1000000007;
    ans = (ans * ans) % 1000000007;
    if (p % 2 == 0)
      return ans;
    else
      return ans = (ans * n) % 1000000007;
  }
}
bool isPrime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  {
    long long int i, j, n, a, b, c, c1, d, m, sum = 0, temp, k, k2, k1, ans = 0,
                                              cnt = 0, r = 0, e, f;
    string str, stri, om, s;
    unordered_map<long long int, long long int> make_pair, mp1;
    set<long long int> st;
    vector<long long int> v;
    cin >> n >> s;
    for (i = 0; i < n; i++) {
      if (s[i] == ')')
        r++;
      else {
        r--;
        if (r >= 0) cnt++;
      }
    }
    if (r)
      cout << -1 << "\n";
    else {
      cout << (2 * cnt) << "\n";
    }
  }
  return 0;
}
