#include <bits/stdc++.h>
using namespace std;
void c_p_c() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int32_t main() {
  c_p_c();
  long long n;
  cin >> n;
  cin.ignore();
  string s;
  cin >> s;
  long long a[2] = {0};
  for (long long i = 0; i < (n); ++i) {
    if (s[i] == '(')
      a[0]++;
    else
      a[1]++;
  }
  if (a[0] != a[1])
    cout << "-1";
  else {
    long long count = 0;
    long long open = 0, close = 0;
    for (long long i = 0; i < (n); ++i) {
      if (s[i] == '(') open++;
      if (s[i] == ')') {
        if (open > 0)
          open--;
        else {
          close++;
          long long j = i + 1;
          while (j < n and close != 0) {
            if (s[j] == ')') close++;
            if (s[j] == '(') close--;
            j++;
          }
          count += j - i;
          i = j - 1;
        }
      }
    }
    cout << count;
  }
  return 0;
}
