#include <bits/stdc++.h>
using namespace std;
int n;
string s;
vector<int> balance;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  cin >> s;
  balance.resize(n + 1);
  balance[0] = 0;
  int cnto = 0, cntp = 0;
  for (int i = 0; i <= n - 1; ++i) {
    if (s[i] == '(') {
      cnto++;
      balance[i + 1] = balance[i] + 1;
    } else {
      cntp++;
      balance[i + 1] = balance[i] - 1;
    }
  }
  if (cnto != cntp) {
    cout << "-1";
    return 0;
  }
  int reset = 0;
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    if (balance[i] < 0) {
      reset++;
    } else {
      res += reset;
      if (reset > 0) res++;
      reset = 0;
    }
  }
  cout << res;
  return 0;
}
