#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, a[N], tot, x, y, ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    char ch;
    cin >> ch;
    a[i] = (ch == '(');
    tot += a[i];
  }
  if (tot + tot != n) return cout << -1, 0;
  int poi = 1;
  while (poi <= n) {
    int j = poi + 1, ok = 1;
    (a[poi] ? x++ : y++);
    if (x < y) ok = 0;
    while (j <= n && x != y) {
      (a[j++] ? x++ : y++);
      if (x < y) ok = 0;
    }
    if (!ok) {
      for (int l = poi, r = j - 1;; l++, r--) {
        if (!a[l] || a[r]) {
          ans += (r - l + 1);
          break;
        }
      }
    }
    poi = j;
  }
  cout << ans;
}
