#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
void clock_out() {
  cerr << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
}
void fileio() {
  freopen("/home/dwai/Desktop/cp/input.txt", "r", stdin);
  freopen("/home/dwai/Desktop/cp/output.txt", "w", stdout);
  freopen("/home/dwai/Desktop/cp/debug.txt", "w", stderr);
}
void ofileio() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
int IT_MAX = 1 << 20;
const long long MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double ERR = 1e-10;
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long o = 0, c = 0;
  long long i;
  long long ans = 0;
  for (i = 0; i < n; i++) {
    if (s[i] == '(') o++;
    if (s[i] == ')') {
      if (c == 0 && o)
        o--;
      else
        c++;
    }
    if (c == o) {
      ans += 2 * c;
      c = o = 0;
    }
  }
  if (o > 0 || c > 0) {
    cout << -1;
    return;
  }
  cout << ans;
}
int main() {
  42;
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  srand(time(NULL));
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
