#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, l = 0, sum = 0;
  cin >> n;
  string s;
  cin >> s;
  int a[n + 1];
  memset(a, 0, sizeof(a));
  if (s[0] == '(')
    a[0]++;
  else
    a[0]--;
  for (i = 1; i < n; i++)
    if (s[i] == '(')
      a[i] = a[i - 1] + 1;
    else
      a[i] = a[i - 1] - 1;
  for (i = 0; i < n; i++) {
    if (a[i] == -1) {
      while (a[i] != 0) {
        l++;
        i++;
      }
      l++;
    }
  }
  if (n % 2 == 1 || a[n - 1] != 0)
    cout << "-1\n";
  else
    cout << l << "\n";
  return 0;
}
