#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int bal = 0;
  int c = 0;
  int len = 0;
  for (int i = 0; i < n; i++) {
    bal += s[i] == ')' ? -1 : 1;
    if (bal < 0) {
      c += 1;
    }
    if (bal == 0 and s[i] == '(') {
      len += c + 1;
      c = 0;
    }
  }
  if (bal != 0)
    cout << "-1";
  else
    cout << len;
  return 0;
}
