#include <bits/stdc++.h>
using namespace std;
template <class X = int>
inline X min(X a, X b, X c) {
  return min(min(a, b), c);
}
template <class X = int>
inline X min(X a, X b, X c, X d) {
  return min(min(a, b, c), d);
}
template <class X = int>
inline X max(X a, X b, X c) {
  return max(max(a, b), c);
}
template <class X = int>
inline X max(X a, X b, X c, X d) {
  return max(max(a, b, c), d);
}
template <class X = int>
inline X mid(X s, X e) {
  return (s + (e - s) / 2);
}
template <class X = int>
inline X len(X s, X e) {
  return (e - s + 1);
}
const int MOD = 1e9 + 7;
const int INF = 987654321;
const bool TESTCASES = 0;
const bool CODEJAM = 0;
void solve() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  int ans = 0;
  int temp = 0, br = 0;
  for (int i = 0; i < n; i++) {
    br += s[i] == ')' ? -1 : 1;
    if (br < 0)
      temp++;
    else if (br == 0 and temp != 0) {
      ans += temp + 1;
      temp = 0;
    }
  }
  if (br != 0)
    cout << "-1\n";
  else
    cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T = 1;
  if (TESTCASES) cin >> T;
  for (int t = 1; t <= T; t++) {
    if (CODEJAM) cout << "Case #" << t << ": ";
    solve();
  }
  return 0;
}
