#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  deque<int> stk;
  long long i = 0, cf = 0, cs = 0, cost = 0;
  for (int i = 0; i < n; ++i) {
    if (str[i] == ')')
      cs++;
    else
      cf++;
  }
  if (cf != cs) {
    cout << "-1\n";
    return;
  }
  cf = 0;
  cs = 0;
  while (i < n) {
    if (stk.empty() or str[stk.back()] == ')' or str[i] == '(') {
      stk.push_back(i);
      if (str[i] == ')')
        cs++;
      else
        cf++;
      if (cs == cf) {
        cost += stk.back() - stk.front() + 1;
        cf = 0;
        cs = 0;
        stk.clear();
      }
      i++;
      continue;
    }
    cf--;
    stk.pop_back();
    i++;
  }
  cout << cost << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T, n, k;
  T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
