#include <bits/stdc++.h>
using namespace std;
const int maxn = 500000 + 200;
vector<int> sum[2 * maxn];
char S[maxn];
int n;
int m = 2;
int s[maxn], sa[maxn], t[maxn], t2[maxn], c[maxn];
int Rank[maxn], height[maxn];
int num[maxn];
void getheight() {
  int i, j, k = 0;
  for (int i = 0; i < n; ++i) Rank[sa[i]] = i;
  for (int i = 0; i < n; ++i) {
    k = (k == 0) ? 0 : k - 1;
    if (Rank[i] != 0) {
      while (s[i + k] == s[sa[Rank[i] - 1] + k]) k++;
    }
    height[Rank[i]] = k;
  }
}
void build_sa() {
  int i, *first = t, *second = t2;
  for (int i = 0; i < m; i++) c[i] = 0;
  for (int i = 0; i < n; i++) c[first[i] = s[i]]++;
  for (int i = 1; i < m; i++) c[i] += c[i - 1];
  for (int i = n - 1; i >= 0; i--) sa[--c[first[i]]] = i;
  for (int k = 1; k <= n; k <<= 1) {
    int p = 0;
    for (int i = n - k; i < n; i++) second[p++] = i;
    for (int i = 0; i < n; i++)
      if (sa[i] >= k) second[p++] = sa[i] - k;
    for (int i = 0; i < m; i++) c[i] = 0;
    for (int i = 0; i < n; i++) c[first[second[i]]]++;
    for (int i = 1; i < m; i++) c[i] += c[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--c[first[second[i]]]] = second[i];
    swap(first, second);
    p = 1;
    first[sa[0]] = 0;
    for (int i = 1; i < n; i++) {
      first[sa[i]] = (second[sa[i - 1]] == second[sa[i]] &&
                      second[sa[i - 1] + k] == second[sa[i] + k])
                         ? p - 1
                         : p++;
    }
    if (p >= n) break;
    m = p;
  }
}
long long query(int L, int R) {
  long long ans = 0;
  if (L >= R || S[L] == ')') return 0LL;
  int fst = num[L] - 1;
  auto it = upper_bound(sum[fst].begin(), sum[fst].end(), L);
  auto itt = upper_bound(sum[fst].begin(), sum[fst].end(), R);
  if (it == sum[fst].end()) return 0LL;
  if (itt == sum[fst].end())
    R = n - 1;
  else
    R = *itt;
  auto fin = upper_bound(sum[fst - 1].begin(), sum[fst - 1].end(), L);
  if (fin == sum[fst - 1].end()) return (long long)(itt - it);
  if (*fin < R) {
    R = (*fin);
    itt = upper_bound(sum[fst].begin(), sum[fst].end(), R);
    return (long long)(itt - it);
  } else {
    return (long long)(itt - it);
  }
}
int main() {
  scanf("%d", &n);
  scanf("%s", S);
  int now = 0;
  for (int i = 0; i < n; ++i) {
    if (S[i] == '(')
      ++now, s[i] = 0;
    else
      --now, s[i] = 1;
    num[i] = now + maxn;
    sum[now + maxn].push_back(i);
  }
  m = 2;
  s[n] = 10;
  build_sa();
  getheight();
  long long ans = 0;
  for (int suffix = n - 1; suffix >= 0; --suffix) {
    ans +=
        query(suffix, n - 1) - query(suffix, suffix + height[Rank[suffix]] - 1);
  }
  cout << ans << endl;
  return 0;
}
