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