#include <bits/stdc++.h>
using namespace std;
long long int rvsr(long long int n) {
  string s = to_string(n);
  reverse(s.begin(), s.end());
  stringstream obj(s);
  obj >> n;
  return n;
}
long long int power(long long int x, long long int y) {
  long long int res = 1;
  x = x;
  while (y > 0) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
long long int n;
int main() {
  cin >> n;
  char ch;
  long long int i, a = 0, b = 0;
  if (n & 1) {
    cout << -1;
    exit(0);
  }
  std::vector<char> v;
  for (i = 0; i < n; i++) {
    cin >> ch;
    (ch == '(') ? a++ : b++;
    v.push_back(ch);
  }
  if (a != b) {
    cout << -1;
    exit(0);
  }
  a = 0;
  b = 0;
  for (i = 0; i < n; i++) {
    if (v[i] == '(')
      a++;
    else
      a--;
    if (a < 0 || a == 0 && v[i] == '(') b++;
  }
  cout << (a ? -1 : b);
}
