#include <bits/stdc++.h>
const int N = 1000005, W = 19;
std::vector<int> v[2 * N], e[N];
int n, bit[N], st[N][W], suf[N], a[N], q, l, r, last = 1, cnt = 1;
int len[N], ch[N][2], fa[N], pos[N];
long long ans;
char s[N];
void init() {
  for (int i = 2; i <= n; i++) bit[i] = bit[i >> 1] + 1;
  for (int i = 1; i <= n; i++) st[i][0] = suf[i];
  for (int i = 1; i <= W; i++)
    for (int j = 1; j + (1 << i) - 1 <= n; j++)
      st[j][i] = std::min(st[j][i - 1], st[j + (1 << i - 1)][i - 1]);
}
int qry(int l, int r) {
  int len = bit[r - l + 1];
  return std::min(st[l][len], st[r - (1 << len) + 1][len]);
}
void insert(int x) {
  int p = last, np = ++cnt;
  last = np;
  len[np] = len[p] + 1;
  for (; p && !ch[p][x]; p = fa[p]) ch[p][x] = np;
  if (!p) {
    fa[np] = 1;
    return;
  }
  int q = ch[p][x];
  if (len[q] == len[p] + 1) {
    fa[np] = q;
    return;
  }
  int newq = ++cnt;
  ch[newq][0] = ch[q][0];
  ch[newq][1] = ch[q][1];
  fa[newq] = fa[q];
  len[newq] = len[p] + 1;
  fa[q] = fa[np] = newq;
  for (; p && ch[p][x] == q; p = fa[p]) ch[p][x] = newq;
}
int find(int l, int r, int p) {
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (qry(mid, p) >= suf[p + 1])
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return ans;
}
void dfs(int x, int fa) {
  for (auto u : e[x]) {
    dfs(u, x);
    if (!pos[x]) pos[x] = pos[u];
  }
  int p = pos[x], l = p - len[x] + 1, r = p - (len[fa] + 1) + 1;
  int tx = suf[p + 1];
  int t = find(l, r, p);
  if (!t) return;
  int pl = std::lower_bound(v[tx + n].begin(), v[tx + n].end(), t) -
           v[tx + n].begin();
  int pr = std::upper_bound(v[tx + n].begin(), v[tx + n].end(), r) -
           v[tx + n].begin();
  ans = ans + pr - pl;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] == '(')
      a[i] = 0, suf[i] = -1;
    else
      a[i] = 1, suf[i] = 1;
  for (int i = n; i >= 1; i--) suf[i] += suf[i + 1];
  for (int i = 1; i <= n; i++) v[suf[i] + n].push_back(i);
  for (int i = 0; i <= 2 * n; i++) v[i].push_back(n + 1);
  init();
  for (int i = 1; i <= n; i++) {
    insert(a[i]);
    pos[last] = i;
  }
  for (int i = 2; i <= cnt; i++) e[fa[i]].push_back(i);
  dfs(1, 0);
  printf("%lld\n", ans);
}
