#include <bits/stdc++.h>
using namespace std;
bool first_check(string str) {
  int opened = 0;
  int closed = 0;
  for (char ch : str) {
    if (ch == '(') {
      opened += 1;
    } else {
      closed += 1;
    }
  }
  return (opened == closed);
}
int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  if (first_check(str)) {
    int total_length = 0;
    int opened = 0;
    int closed = 0;
    int we_had_problems_from = -1;
    for (int i = 0; i < n; ++i) {
      char ch = str[i];
      if (ch == '(') {
        opened += 1;
      } else {
        closed += 1;
      }
      if (closed <= opened) {
        if (we_had_problems_from != -1) {
          total_length += (i - we_had_problems_from + 1);
          we_had_problems_from = -1;
        }
      } else {
        if (we_had_problems_from == -1) {
          we_had_problems_from = i;
        }
      }
    }
    cout << total_length << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
