#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j, l = 0, r = 0;
  string s;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    if (s[i] == '(')
      l++;
    else
      l--;
    if (l < 0 || l == 0 && s[i] == '(') r++;
  }
  if (l == 0)
    cout << r << endl;
  else
    cout << -1 << endl;
}
