#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  int sol = 0, sum = 0, lst = -1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      if (sum == -1) sol += i - lst;
      sum++;
    } else
      sum--;
    if (sum == 0) lst = i;
  }
  cout << (sum == 0 ? sol : -1) << "\n";
  return 0;
}
