#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
long long fact[1000000];
void factorial() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i < 1000000; i++)
    fact[i] = (i * (fact[i - 1]) % 1000000007 + 1000000007) % 1000000007;
}
signed main() {
  int T = 1;
  while (T--) {
    long long n;
    cin >> n;
    if (n & 1) {
      cout << "-1";
      return 0;
    }
    string str;
    cin >> str;
    int count = 0, ans = 0, previous;
    for (auto it : str) {
      previous = count;
      if (it == ')')
        count--;
      else
        count++;
      if (count < 0 || previous < 0) ans++;
    }
    if (count)
      cout << -1;
    else
      cout << ans;
  }
  return 0;
}
