#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string a;
  long long int n;
  cin >> n >> a;
  long long int x = 0, sum = 0;
  vector<long long int> b(n);
  for (int i = 0; i < n; i++) {
    if (a[i] == '(')
      x++;
    else
      x--;
    b[i] = x;
  }
  if (b[n - 1] != 0) {
    cout << -1;
    return 0;
  }
  x = 0;
  for (int i = 0; i < n; i++) {
    if (b[i] <= -1)
      x++;
    else {
      if (b[max(0, i - 1)] < 0) sum += x + 1;
      x = 0;
    }
  }
  sum += x;
  cout << sum;
  return 0;
}
