#include <bits/stdc++.h>
const int N = 5e5 + 5;
std::vector<int> g[N], gg[N];
char s[N];
struct SuffixArray {
  int sa[N], rank[N], height[N], cnt[N], a1[N], a2[N], n, m, *x, *y;
  void sort() {
    for (int i = 0; i < m; i++) cnt[i] = 0;
    for (int i = 0; i < n; i++) cnt[x[i]]++;
    for (int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--cnt[x[y[i]]]] = y[i];
  }
  void build(char *s, int c_size) {
    n = strlen(s);
    m = c_size;
    x = a1;
    y = a2;
    for (int i = 0; i < n; i++) x[i] = s[i], y[i] = i;
    x[n] = y[n] = -1;
    sort();
    for (int k = 1; k <= n; k <<= 1) {
      int p = 0;
      for (int i = n - k; i < n; i++) y[p++] = i;
      for (int i = 0; i < n; i++)
        if (sa[i] >= k) y[p++] = sa[i] - k;
      sort();
      p = 0;
      std::swap(x, y);
      x[sa[0]] = 0;
      for (int i = 1; i < n; i++) {
        if (y[sa[i]] != y[sa[i - 1]] || y[sa[i] + k] != y[sa[i - 1] + k]) p++;
        x[sa[i]] = p;
      }
      if (p + 1 >= n) break;
      m = p + 1;
    }
    for (int i = 0; i < n; i++) rank[sa[i]] = i;
    height[0] = 0;
    int k = 0;
    for (int i = 0; i < n; i++) {
      if (k) k--;
      if (rank[i] == 0) continue;
      int j = sa[rank[i] - 1];
      while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
      height[rank[i]] = k;
    }
  }
} SA;
int f[N][21], n, sum[N];
int query(int l, int r) {
  int k = floor(log2(1.0 * r - l + 1));
  return std::min(f[l][k], f[r - (1 << k) + 1][k]);
}
int main() {
  scanf("%d%s", &n, s);
  sum[0] = 0;
  std::map<int, int> t;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1];
    if (s[i - 1] == '(')
      sum[i]++;
    else
      sum[i]--;
    t[sum[i]]++;
  }
  long long ans = 0;
  SA.build(s, 128);
  int k = 0;
  while ((1 << (k + 1)) <= n) k++;
  for (int i = 1; i <= n; i++) f[i][0] = sum[i];
  for (int i = 1; i <= k; i++)
    for (int j = 1; j + (1 << i) - 1 <= n; j++)
      f[j][i] = std::min(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
  for (int i = 1; i <= n; i++) {
    int l = SA.height[SA.rank[i - 1]], r = n - i + 2, mid;
    int p = i + l;
    if (s[i - 1] == '(' && query(i, p) >= sum[i - 1]) {
      while (r - l > 1) {
        mid = (l + r) >> 1;
        if (query(p, i + mid) >= sum[i - 1])
          l = mid;
        else
          r = mid;
      }
      g[i + r].push_back(sum[i - 1]);
      gg[p].push_back(sum[i - 1]);
    }
    for (int v : g[i]) ans -= t[v];
    for (int v : gg[i]) ans += t[v];
    t[sum[i]]--;
  }
  printf("%lld\n", ans);
  return 0;
}
