#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
void read(vector<long long int> &a, int n) {
  a.resize(n);
  for (int i = 0; i < n; i++) cin >> a[i];
}
bool fine = 0;
vector<long long int> a;
long long int n, m, k;
void solve() {
  long long int a = 0, b = 0;
  string s;
  cin >> n;
  cin >> s;
  vector<int> id(n);
  id[0] = (s[0] == ')' ? -1 : 1);
  for (int i = 0; i < n; i++) {
    if (s[i] == ')')
      b++;
    else
      a++;
    if (i) {
      if (s[i] == ')')
        id[i] = id[i - 1] - 1;
      else
        id[i] = id[i - 1] + 1;
    }
  }
  if (a != b) {
    cout << -1 << "\n";
    return;
  }
  bool find = false;
  long long int ans = 0;
  int last;
  for (int i = 0; i < n; i++) {
    if (id[i] == -1 && !find) {
      find = 1;
      last = i;
    }
    if (id[i] == 0 && find) {
      find = 0;
      ans += i - last + 1;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int i = 1;
  if (fine) {
    int t;
    cin >> t;
    while (t--) {
      solve();
    }
  } else
    solve();
  return 0;
}
