#include <bits/stdc++.h>
using namespace std;
long long denom = 1e9 + 7;
long long temp = 0;
int main(int argc, const char* argv[]) {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  if (n % 2 == 1) {
    cout << -1;
  } else {
    long long open = 0;
    long long close = 0;
    long long prev = -1;
    long long result = 0;
    for (long long i = (0); i < (n); i++) {
      s[i] == '(' ? open++ : close++;
      if (open == close) {
        if (prev != -1) {
          result += (i - prev + 1);
          prev = -1;
        }
      } else if (close > open) {
        if (prev == -1) prev = i;
      }
    }
    if (open != close)
      cout << -1;
    else
      cout << result;
  }
  return 0;
}
