#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long open = count(s.begin(), s.end(), '(');
  if (open != n - open) {
    cout << -1 << "\n";
    return 0;
  }
  long long count = 0, ans = 0;
  stack<char> st;
  for (char ch : s) {
    if (ch == '(') {
      if (count == 0) {
        st.push(ch);
      } else {
        count++;
        ans++;
      }
    } else {
      if (st.empty()) {
        count--;
        ans++;
      } else if (st.top() == '(') {
        st.pop();
      }
    }
  }
  cout << ans << "\n";
}
