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