#include <bits/stdc++.h>
using namespace std;
bool prime(long long x) {
  if (x == 2) {
    return true;
  }
  if (x <= 1) {
    return true;
  }
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
void printv(long long v[], long long n) {
  for (long long i = 0; i < n; i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
string Y = "YES\n";
string N = "NO\n";
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  string s;
  cin >> t >> s;
  long long cnt = 0;
  long long poi = 0;
  int flag = 0;
  long long i = 0;
  while (i < s.size()) {
    if (s[i] == '(') {
      poi++;
    } else {
      poi--;
    }
    if (poi < 0) {
      flag = 1;
      cnt++;
    }
    if (flag == 1 && poi == 0) {
      cnt++;
      flag = 0;
    }
    i++;
  }
  if (poi != 0) {
    cout << -1 << endl;
  } else {
    cout << cnt << endl;
  }
}
