#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 0, counter = 0, check = 0;
  cin >> n;
  string brakests = "";
  cin >> brakests;
  if (brakests.length() % 2 == 0) {
    for (int i = 0; i < brakests.length(); i++) {
      if (brakests[i] == '(')
        check++;
      else
        check--;
      if (check < 0 || check == 0 && brakests[i] == '(') counter++;
    }
    if (check == 0)
      cout << counter;
    else
      cout << -1;
  } else {
    cout << -1;
  }
}
