#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9 + 1e8;
const long long INF = 2e18 + 1e17;
const int mod = 998244353;
const int MOD = 1e9 + 7;
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
bool is_prime(int x) {
  if (x <= 1 || (x % 2 == 0 && x > 2)) return false;
  for (int i = 3; i * i <= x; i += 2) {
    if (!(x % i)) {
      return false;
    }
  }
  return true;
}
const int MAXN = 40010;
int N;
string str;
vector<int> arr;
int ans;
int main() {
  cin >> N >> str;
  arr.resize(N);
  int sum = 0;
  for (int i = 0; i < str.length(); i++) {
    arr[i] = (str[i] == ')' ? -1 : 1);
    sum += arr[i];
  }
  if (sum != 0) {
    cout << "-1" << endl;
    return 0;
  }
  int cur = 0, last = 0;
  bool bad = false;
  for (int i = 0; i < N; i++) {
    cur += arr[i];
    if (!bad && cur < 0) {
      bad = true;
      last = i;
    } else if (cur == 0 && bad) {
      ans += (i - last + 1);
      bad = false;
    }
  }
  cout << ans << endl;
  return 0;
}
