#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> a(n + 1, 0);
  for (int i = 0; i <= n; i++) {
    if (s[i] == '(')
      a[i + 1] = a[i] + 1;
    else
      a[i + 1] = a[i] - 1;
  }
  if (a[n] != 0) {
    cout << "-1" << endl;
  } else {
    int ans = 0;
    for (int i = 0, zero = 0; i <= n; i++) {
      if (a[i] < 0) {
        ans++;
        zero = 1;
      }
      if (a[i] == 0 && zero == 1) {
        ans++;
        zero = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
