#include <bits/stdc++.h>
using namespace std;
const long long int inf = (long long int)1e18 + 19;
long long int mini(long long int a[], long long int b[], long long int l,
                   long long int r, long long int s1, long long int k) {
  long long int mid = (l + r) / 2;
  while (l <= r) {
  }
}
void cazz() {
  long long int t, n, i, j, k, cnt = 0, q;
  string s;
  cin >> n >> s;
  if (n % 2 != 0) {
    cout << -1;
    return;
  }
  vector<long long int> v1, v2;
  for (long long int i = 0; i < n; i++) {
    if (s[i] == '(')
      v1.push_back(i);
    else
      v2.push_back(i);
  }
  long long int l1 = v1.size(), l2 = v2.size();
  if (l1 != l2) {
    cout << -1;
    return;
  }
  long long int c = 0;
  for (long long int i = 0; i < l1; i++) {
    if (v2[i] < v1[i]) c += (v1[i] - v2[i] + 1);
    if ((i + 1) < l1 and (v1[i] - v2[i + 1]) > 0) {
      v2[i + 1] = v1[i];
      c--;
    }
  }
  cout << c;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cazz();
  return 0;
}
