#include <bits/stdc++.h>
using namespace std;
vector<int> V;
int main() {
  int n;
  int cnt0 = 0, cnt1 = 0;
  int ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    char a;
    cin >> a;
    if (a == '(') {
      V.push_back(1);
      cnt0++;
    } else {
      if (!V.empty()) V.pop_back();
      cnt1++;
    }
    if (cnt0 == cnt1) {
      if (!V.empty()) ans += 2 * cnt0;
      V.clear();
      cnt0 = 0;
      cnt1 = 0;
    }
  }
  if (cnt1 != cnt0)
    cout << "-1";
  else
    cout << ans;
}
