#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long diffIndex = 0, ans = 0, op = 0, cl = 0, previ = 0;
  for (long long i = 0; i < s.length(); i++) {
    char ch = s[i];
    if (ch == '(') {
      op++;
      if (op <= cl) diffIndex++;
      if (op == cl) {
        ans += diffIndex;
        diffIndex = 0;
      }
    } else {
      cl++;
      if (op < cl) diffIndex++;
    }
  }
  if (op == cl)
    cout << ans;
  else
    cout << -1;
  cout << '\n';
  return 0;
}
