#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const int mod = 1e9 + 7;
int t, n, m, ans;
char s[N];
void solve() {
  cin >> n;
  cin >> s + 1;
  int l = 0, r = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '(')
      ++l;
    else
      ++r;
    if (l == r && l && s[i] == '(') {
      ans += (2 * l);
      l = r = 0;
    } else if (l == r && l && s[i] == ')') {
      l = r = 0;
    }
  }
  if (l != r) {
    cout << -1 << endl;
  } else
    cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
