#include <bits/stdc++.h>
using namespace std;
long long _gcd(long long x, long long y) { return (!y ? x : _gcd(y, x % y)); }
long long _lcm(long long x, long long y) { return x / _gcd(x, y) * y; }
int main() {
  std::ios_base::sync_with_stdio(0);
  long long n;
  string s;
  cin >> n >> s;
  long long a = 0, b = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == ')')
      a++;
    else
      b++;
  }
  if (n % 2 || a != b) return cout << -1 << "\n", 0;
  a = 0, b = 0;
  int inx1 = 0, inx2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      a++;
      inx1 = i;
    } else {
      b++;
      inx2 = i;
    }
    if (a == b) {
      if (inx2 < inx1) ans += a * 2;
      a = 0;
      b = 0;
    }
  }
  cout << ans << "\n";
}
