#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  srand(time(NULL));
  ;
  long long t, n, m, k;
  t = 1;
  while (t--) {
    cin >> n;
    string s;
    cin >> s;
    bool flag = false;
    long long sum = 0, ans = 0;
    for (long long i = 0; i < n; i++) {
      if (s[i] == '(')
        sum++;
      else
        sum--;
      if (sum < 0) flag = true;
      ans += flag;
      if (sum == 0) flag = false;
    }
    if (sum == 0)
      cout << ans;
    else
      cout << -1;
  }
  return 0;
}
