#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265359;
int main() {
  int n;
  cin >> n;
  string operation;
  cin >> operation;
  int number = 0, fnumber = 0;
  for (int i = 0; i < n; i++) {
    if (operation.at(i) == '(')
      number++;
    else if (operation.at(i) == ')')
      number--;
    if (number < 0 || (number == 0 && operation.at(i) == '(')) fnumber++;
  }
  int mone = -1;
  cout << (number == 0 ? fnumber : mone) << endl;
  return 0;
}
