#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string S;
  cin >> S;
  int Sum = 0;
  int ans = 0;
  for (char c : S) {
    if (c == '(') Sum++;
    if (c == ')') Sum--;
    if (Sum < 0) ans++;
    if (Sum == -1 && c == ')') ans++;
  }
  if (Sum) {
    cout << -1;
    return 0;
  }
  cout << ans << endl;
}
