#include <bits/stdc++.h>
using namespace std;
char s[500010];
int n, a[500010], mn[500010][20], rk[2][500010], sa[2][500010], h[500010],
    v[500010], top;
long long res;
int ll[1000010], rr[1000010];
int p = 0, q = 1, k = 1;
struct node {
  int v, id;
} st[500010];
inline bool operator<(node a, node b) {
  if (a.v != b.v) return a.v < b.v;
  return a.id < b.id;
}
inline int query(int l, int r) {
  int t = log2(r - l + 1);
  return min(mn[l][t], mn[r - (1 << t) + 1][t]);
}
inline void calsa(int sa[], int rk[], int SA[], int RK[]) {
  for (int i = 1; i <= n; i++) v[rk[sa[i]]] = i;
  for (int i = n; i >= 1; i--) {
    if (sa[i] > k) SA[v[rk[sa[i] - k]]--] = sa[i] - k;
  }
  for (int i = n - k + 1; i <= n; i++) SA[v[rk[i]]--] = i;
  for (int i = 1; i <= n; i++)
    RK[SA[i]] = RK[SA[i - 1]] + (rk[SA[i]] != rk[SA[i - 1]] ||
                                 rk[SA[i] + k] != rk[SA[i - 1] + k]);
}
inline int sum(int L, int R) {
  int l = L, r = R, pos = 0, lp = 0, rp = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (query(l, mid) - a[L - 1] < 0)
      r = mid - 1;
    else
      pos = mid, l = mid + 1;
  }
  if (!pos || !ll[a[L - 1] + 500000]) return 0;
  l = ll[a[L - 1] + 500000], r = rr[a[L - 1] + 500000];
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (st[mid].id < L)
      l = mid + 1;
    else
      lp = mid, r = mid - 1;
  }
  l = ll[a[L - 1] + 500000], r = rr[a[L - 1] + 500000];
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (st[mid].id > pos)
      r = mid - 1;
    else
      rp = mid, l = mid + 1;
  }
  if (lp > rp || !lp || !rp) return 0;
  return rp - lp + 1;
}
inline int ha(char x) {
  if (x == '(')
    return 2;
  else
    return 1;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      a[i] = 1;
    else
      a[i] = -1;
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= n; i++) st[i] = (node){a[i], i};
  sort(st + 1, st + n + 1);
  for (int i = 1; i <= n; i++) rr[st[i].v + 500000] = i;
  for (int i = n; i >= 1; i--) ll[st[i].v + 500000] = i;
  for (int i = 1; i <= n; i++) mn[i][0] = a[i];
  for (int j = 1; j <= 19; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]);
    }
  }
  for (int i = 1; i <= n; i++) v[ha(s[i])]++;
  for (int i = 1; i <= 26; i++) v[i] += v[i - 1];
  for (int i = 1; i <= n; i++) sa[p][v[ha(s[i])]--] = i;
  for (int i = 1; i <= n; i++)
    rk[p][sa[p][i]] = rk[p][sa[p][i - 1]] + (s[sa[p][i]] != s[sa[p][i - 1]]);
  while (k < n) {
    calsa(sa[p], rk[p], sa[q], rk[q]);
    swap(p, q);
    k <<= 1;
  }
  k = 0;
  for (int i = 1; i <= n; i++) {
    if (rk[p][i] == 1)
      h[rk[p][i]] = 0;
    else {
      int j = sa[p][rk[p][i] - 1];
      while (s[j + k] == s[i + k]) k++;
      h[rk[p][i]] = k;
      if (k > 0) k--;
    }
  }
  res += sum(sa[p][1], n);
  for (int i = 2; i <= n; i++) {
    res += sum(sa[p][i], n) - sum(sa[p][i], sa[p][i] + h[i] - 1);
  }
  printf("%lld\n", res);
  return 0;
}
