#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, k = 0, x = 0;
  int y = 0, f = 0, sum = 0;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      x++;
    else
      x--;
  }
  if (x != 0)
    cout << -1;
  else {
    x = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '(') {
        x++;
        if (x == y) {
          sum += i - k + 1;
          k = i + 1;
          f = x = y = 0;
        }
      } else {
        y++;
        if (x == y) {
          if (f) {
            sum += i - k + 1;
            k = i + 1;
            f = x = y = 0;
          } else {
            k = i + 1;
            f = x = y = 0;
          }
        } else if (x < y) {
          f = 1;
        }
      }
    }
    cout << sum;
  }
  return 0;
}
