#include <bits/stdc++.h>
using namespace std;
namespace SA {
const int MAXN = 1 << 20;
char* S;
int N, gap;
int sa[MAXN], pos[MAXN], tmp[MAXN], lcp[MAXN];
bool sufCmp(int i, int j) {
  if (pos[i] != pos[j]) return pos[i] < pos[j];
  i += gap;
  j += gap;
  return (i < N && j < N) ? pos[i] < pos[j] : i > j;
}
void buildSA() {
  N = strlen(S);
  for (int i = 0; i < (int)(N); ++i) sa[i] = i, pos[i] = S[i];
  for (gap = 1;; gap *= 2) {
    sort(sa, sa + N, sufCmp);
    for (int i = 0; i < (int)(N - 1); ++i)
      tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
    for (int i = 0; i < (int)(N); ++i) pos[sa[i]] = tmp[i];
    if (tmp[N - 1] == N - 1) break;
  }
}
void buildLCP() {
  for (int i = 0, k = 0; i < N; ++i)
    if (pos[i] != N - 1) {
      for (int j = sa[pos[i] + 1]; S[i + k] == S[j + k];) ++k;
      lcp[pos[i]] = k;
      if (k) --k;
    }
}
}  // namespace SA
char s[500005];
int v[500005];
int n;
int nkless[500005];
vector<int> vls[1000005];
int cwv(int vl, int s, int e) {
  if (e - s <= 0) return 0;
  vl += 500002;
  return lower_bound(vls[vl].begin(), vls[vl].end(), e) -
         lower_bound(vls[vl].begin(), vls[vl].end(), s);
}
int proc(int i, int mj) {
  int ej = nkless[i];
  return cwv(v[i], mj, ej + 1);
}
int main() {
  scanf("%d %s", &n, s);
  SA::S = s;
  SA::buildSA();
  SA::buildLCP();
  for (int i = (0); i < (n); i++) {
    v[i + 1] = v[i];
    if (s[i] == '(')
      v[i + 1]++;
    else
      v[i + 1]--;
  }
  for (int i = (0); i < (n + 1); i++) {
    vls[v[i] + 500002].push_back(i);
  }
  vector<pair<int, int> > stk;
  stk.emplace_back(-1e8, n);
  for (int i = n - 1; i >= 0; i--) {
    while (stk.back().first >= v[i]) stk.pop_back();
    nkless[i] = stk.back().second;
    stk.emplace_back(v[i], i);
  }
  int clen = 0;
  long long res = 0;
  for (int z = (0); z < (n); z++) {
    int i = SA::sa[z];
    int a = proc(i, i + clen + 1);
    res += a;
    clen = SA::lcp[z];
  }
  printf("%lld\n", res);
}
