#include <bits/stdc++.h>
using namespace std;
long long pw(long long x, long long a) {
  if (a == 0) return 1;
  long long t = pw(x, a / 2);
  if (a % 2 == 0)
    return (t * t);
  else
    return (t * t * x);
}
bool prime(int x) {
  if (x < 2) return false;
  for (int i = 2; i <= sqrt(x); i++) {
    if ((x % i) == 0) return false;
  }
  return true;
}
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first < b.first);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string x;
  cin >> x;
  int ind = 0;
  int dif = 0;
  bool cut = 0;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '(')
      dif++;
    else
      dif--;
    if (dif < 0) cut = 1;
    if (dif == 0 && cut) {
      ans += i - ind + 1;
      ind = i + 1;
      cut = 0;
    }
    if (dif == 0) ind = i + 1;
  }
  if (dif != 0)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
