#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int p = 0, q = 0, r = 0, s = 0, j = 0;
  char x;
  cin >> n;
  vector<char> v;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x == '(')
      p++;
    else
      q++;
    if (q > p) j++;
    if (q == p && s > r) j++;
    r = p;
    s = q;
  }
  if (p != q)
    cout << "-1";
  else
    cout << j;
  return 0;
}
