#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  string s;
  cin >> s;
  long long c = 0, c1 = 0, c2 = 0, p = 0;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == '(') {
      c1 += 1;
      c += 1;
    } else if (s[i] == ')') {
      c2 += 1;
      c -= 1;
      if (c < 0) p++;
    }
  }
  if (c != 0)
    cout << "-1" << endl;
  else
    cout << (p * 2) << endl;
  return 0;
}
