#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int a[N];
int n, m, K;
string s;
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  cin >> s;
  for (int i = 1; i <= n; ++i) {
    a[i] = a[i - 1];
    if (s[i - 1] == '(')
      a[i]++;
    else
      a[i]--;
  }
  if (a[n]) return puts("-1"), 0;
  int i = 1, ans = 0;
  while (i <= n) {
    if (a[i] != -1) {
      i++;
      continue;
    }
    int j = i;
    while (j <= n && a[j]) {
      j++;
    }
    ans += j - i + 1;
    i = j;
  }
  cout << ans << endl;
  return 0;
}
