#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e6 + 1;
const long long PRIME = 1e9 + 7;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  if (n & 1) {
    cout << "-1\n";
    return;
  }
  bool flag = 0;
  long long sum = 0, ans = 0;
  long long pos = -1;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '(')
      sum++;
    else
      sum--;
    if (sum < 0 && flag == 0) {
      flag = 1;
      pos = i;
    }
    if (flag == 1 && sum == 0) {
      flag = 0;
      ans += i - pos + 1;
    }
  }
  if (sum != 0) {
    cout << -1 << "\n";
    return;
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
}
