#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  vector<long long int> v(n + 1, 0);
  for (long long int i = 1; i <= n; i++) {
    if (s[i - 1] == '(')
      v[i] = v[i - 1] + 1;
    else
      v[i] = v[i - 1] - 1;
  }
  if (v[n] != 0) {
    cout << "-1" << '\n';
    return 0;
  }
  long long int ans = 0;
  long long int temp = 0;
  for (long long int i = 1; i <= n; i++) {
    if (v[i] < 0) {
      temp++;
    } else if (v[i] == 0 && v[i - 1] < 0) {
      temp++;
      ans += temp;
      temp = 0;
    }
  }
  cout << ans << '\n';
  return 0;
}
