#include <bits/stdc++.h>
using namespace std;
bool flag = 0, flag1 = 0, flag2 = 0;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    string s;
    cin >> s;
    long long int ans = 0, cnt = 0, p[s.length()];
    for (long long int i = 0; i < s.length(); i++) {
      if (s[i] == '(')
        cnt++;
      else
        cnt--;
      p[i] = cnt;
    }
    if (cnt != 0) {
      cout << -1;
      return 0;
    }
    for (long long int i = 0; i < s.length(); i++) {
      if (p[i] < 0) ans++;
      if (p[i] == 0 && p[i - 1] < 0) ans++;
    }
    cout << ans;
  }
  return 0;
}
