#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  stack<int> st;
  long long ans = 0;
  int cnt = 0;
  for (int i = 0; i < n; ++i) cnt += s[i] == '(' ? 1 : -1;
  if (cnt != 0) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '(') {
      st.push(1);
    } else {
      if (st.empty()) {
        int excess = -1;
        int j = i + 1;
        while (excess != 0 && j < n) {
          excess += s[j] == '(' ? 1 : -1;
          ++j;
        }
        --j;
        ans += j - i + 1;
        i = j;
      } else {
        st.pop();
      }
    }
  }
  cout << ans << endl;
}
