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