#include <bits/stdc++.h>
using namespace std;
int lst = 1, cn = 1, fail[1000010], len[1000010], ch[1000010][2], sz[1000010],
    id[1000010];
void insert(int c, int t) {
  int p = lst, np = lst = ++cn;
  len[np] = len[p] + 1;
  sz[np] = 1;
  id[np] = t;
  for (; p && !ch[p][c]; p = fail[p]) ch[p][c] = np;
  if (!p)
    fail[np] = 1;
  else {
    int q = ch[p][c];
    if (len[q] == len[p] + 1)
      fail[np] = q;
    else {
      int cl = ++cn;
      len[cl] = len[p] + 1;
      for (int i = 0; i < 2; i++) ch[cl][i] = ch[q][i];
      fail[cl] = fail[q];
      fail[q] = fail[np] = cl;
      for (; p && ch[p][c] == q; p = fail[p]) ch[p][c] = cl;
    }
  }
}
char c[1000010];
int n, q;
vector<int> g[1000010], t[1000010];
struct qq {
  int len, x, fl;
} a[2000010];
bool cmp(qq a, qq b) { return a.x < b.x; }
void dfs(int x) {
  for (int i = 0, v; i < g[x].size(); i++) {
    dfs(v = g[x][i]);
    if (sz[v]) id[x] = id[v];
    sz[x] += sz[v];
  }
  if (x == 1 || !sz[x]) return;
  a[++q] = (qq){len[x], id[x], 1};
  if (len[fail[x]]) a[++q] = (qq){len[fail[x]], id[x], -1};
}
int s[500010], h[500010], top;
long long ans;
int main() {
  cin >> n;
  scanf("%s", c + 1);
  for (int i = 1; i <= n; i++)
    insert(c[i] == '(', i), s[i] = s[i - 1] + ((c[i] == '(') ? 1 : -1);
  for (int i = 2; i <= cn; i++) g[fail[i]].push_back(i);
  dfs(1);
  long long ans = 0;
  sort(a + 1, a + q + 1, cmp);
  int now = 1;
  h[top = 1] = 0;
  t[n].push_back(0);
  for (int i = 1; i <= n; i++) {
    while (top && s[h[top]] >= s[i]) top--;
    int mx = i - h[top];
    h[++top] = i;
    while (now <= q && a[now].x == i) {
      int k = min(a[now].len, mx);
      int sz = t[s[i] + n].size(), l = 0, r = sz - 1, aa = 0;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (i - t[s[i] + n][mid] <= k)
          aa = sz - mid, r = mid - 1;
        else
          l = mid + 1;
      }
      ans += 1ll * a[now++].fl * aa;
    }
    t[s[i] + n].push_back(i);
  }
  return printf("%lld", ans), 0;
}
