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