#include <bits/stdc++.h>
using namespace std;
int32_t main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  scanf("%I64d\n", &n);
  long long c, state = 0, local_len = 0, total_len = 0, bin = 0;
  for (long long i = 0; i < n; i++) {
    c = getchar();
    if (c == '(') {
      state++;
    } else {
      state--;
    }
    if (state < 0) {
      local_len++;
      bin = 1;
    }
    if (state >= 0) {
      if (bin == 1) {
        total_len += local_len + 1;
        local_len = 0;
        bin = 0;
      }
    }
  }
  if (state != 0) {
    cout << "-1";
  } else {
    cout << total_len;
  }
}
