#include <bits/stdc++.h>
using namespace std;
int arr[1000010];
int main() {
  int n;
  scanf("%d%*c", &n);
  for (int i = 0; i < n; i++) {
    char c;
    scanf("%c", &c);
    if (c == ')')
      arr[i] = 0;
    else
      arr[i] = 1;
  }
  int su = 0, ch = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i])
      su++;
    else
      su--;
  }
  if (su != 0) {
    printf("-1");
    return 0;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] == 0)
      su--;
    else
      su++;
    if (su == 0 && arr[i] == 1) ans += i;
    if (su == -1 && arr[i] == 0) ans -= (i - 1);
  }
  printf("%d", ans);
}
