#include <bits/stdc++.h>
using namespace std;
void init() {
  long long n;
  cin >> n;
  string s;
  long long ans = 0, cnt = 0;
  cin >> s;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      cnt++;
    else {
      cnt--;
      if (cnt < 0) ans++;
    }
  }
  if (cnt == 0)
    cout << 2 * ans << endl;
  else
    cout << -1 << endl;
}
int32_t main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  init();
  return 0;
}
