#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  string second;
  cin >> second;
  long long st = -1, end = -1;
  long long bal = 0, bal1 = 0;
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    if (second[i] == '(') {
      bal++;
    } else
      bal--;
  }
  if (bal != 0) {
    cout << "-1";
  } else {
    for (long long i = 0; i < n; i++) {
      if (second[i] == '(') {
        bal++;
      } else
        bal--;
      if (bal1 >= 0 && bal < 0) {
        end = st = i;
        while (bal != 0 && i < n) {
          i++;
          if (second[i] == '(') {
            bal++;
          } else
            bal--;
          end++;
        }
        ans += (end - st + 1);
      }
      bal1 = bal;
    }
    cout << ans;
  }
}
