#include <bits/stdc++.h>
using namespace std;
stack<char> s;
int main() {
  int n;
  cin >> n;
  int ans = 0, cnt1 = 0, cnt2 = 0;
  int fac = 0;
  for (int i = 0; i < n; i++) {
    char a;
    cin >> a;
    if (fac) {
      if (a == '(')
        cnt1++;
      else
        cnt2++;
      if (cnt1 == cnt2) {
        ans += cnt1 * 2;
        cnt1 = cnt2 = 0;
        fac = 0;
      }
    } else {
      if (a == '(')
        s.push('(');
      else if (s.size() != 0)
        s.pop();
      else {
        cnt2++;
        fac = 1;
      }
    }
  }
  if (fac == 1 || s.size() != 0)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
