#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  char dummy;
  for (int i = 0; i < n; i++) {
    cin >> dummy;
    if (dummy == '(') {
      arr[i] = 1;
    } else {
      arr[i] = -1;
    }
  }
  int answer = 0;
  int counter = 0;
  bool event = 0;
  for (int i = 0; i < n; i++) {
    counter += arr[i];
    if (event == 1 && counter == 0) {
      event = 0;
      answer++;
    } else if (counter == -1 || event == 1) {
      event = 1;
      answer++;
    }
  }
  if (counter == 0) {
    cout << answer << endl;
  } else {
    cout << -1 << endl;
  }
}
