#include <bits/stdc++.h>
using namespace std;
long long rr[] = {0, 1, 1, 1, 0, -1, -1, -1};
long long cc[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long mod = 1e9 + 7;
const long long N = 300050;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long test = 1;
  while (test--) {
    long long n, a = 0, b = 0;
    string s;
    cin >> n >> s;
    for (long long i = 0; i < n; i++) {
      if (s[i] == '(') {
        a++;
      } else
        b++;
    }
    if (a != b) return cout << -1, 0;
    stack<char> st;
    a = 0, b = 0;
    long long ans = 0, ok = 0;
    for (long long i = 0; i < n; i++) {
      if ((long long)st.size() == 1 && st.top() == '(' && s[i] == ')') {
        st.pop();
        a = 0, b = 0;
      } else {
        st.push(s[i]);
        if (s[i] == '(')
          a++;
        else
          b++;
        if (a < b) ok = 1;
        if (a == b) {
          while ((long long)st.size()) {
            st.pop();
            if (ok) ans++;
          }
          a = b = 0;
          ok = 0;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
