#include <bits/stdc++.h>
using namespace std;
string s;
int n, ans, sum;
int main() {
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    if (s[i] == ')') {
      sum--;
      if (sum < 0) ans++;
    } else
      sum++;
  }
  if (sum)
    cout << -1;
  else
    cout << ans * 2 << endl;
}
