#include <bits/stdc++.h>
using namespace std;
void solve(int t = 0) {
  int n;
  char ch;
  cin >> n;
  vector<char> v(n);
  for (int i = 0; i < n; i++) {
    cin >> ch;
    if (ch == ')')
      v[i] = -1;
    else
      v[i] = 1;
  }
  if (n & 1) {
    cout << -1;
    cout << "\n";
    return;
  }
  int l = 0, sum = 0, ans = 0, r = 0;
  bool f = 0;
  sum += v[l];
  if (sum < 0) f = 1;
  for (r = 1; r < n; r++) {
    sum += v[r];
    if (sum < 0) f = 1;
    if (sum == 0) {
      if (f) ans += (r - l + 1);
      l = r + 1;
      f = 0;
    }
  }
  if (f) ans += (r - l + 1);
  if (sum != 0)
    cout << -1;
  else
    cout << ans;
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int T = 1;
  for (int t = 0; t < T; t++) solve(t);
}
