#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, cnt = 0, ans = 0, pos = 0;
  cin >> n;
  string str;
  cin >> str;
  long long op = 0, cl = 0;
  if (n & 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (str[i] == ')' && op)
      op--;
    else if (str[i] == ')')
      cl++;
    else if (str[i] == '(')
      op++;
    if (cl == op && op == 0) pos = i + 1;
    if (cl == op && op) {
      ans += abs(pos - i) + 1;
      op = cl = 0;
      pos = i + 1;
    }
  }
  if (op != cl) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}
