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