#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a = 0, k = 0;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      a++;
    else {
      a--;
      if (a < 0) k++;
    }
  }
  if (a != 0)
    cout << "-1" << endl;
  else
    cout << k * 2 << endl;
  return 0;
}
