#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
vector<int> pi[maxn];
char s[maxn];
int has[256], sum[maxn], li[maxn], stk[maxn];
int nxt[maxn][2], par[maxn], len[maxn], id[maxn];
int n, cnt, last, top;
int add(int l) {
  ++cnt;
  len[cnt] = l;
  return cnt;
}
void init() {
  cnt = 0;
  last = add(0);
}
void insert(char ch) {
  int t = has[ch], p = last, cur;
  cur = last = add(len[p] + 1);
  while (p && !nxt[p][t]) nxt[p][t] = cur, p = par[p];
  if (!p) {
    par[cur] = 1;
    return;
  }
  int q = nxt[p][t];
  if (len[q] == len[p] + 1) {
    par[cur] = q;
    return;
  }
  int nq = add(len[p] + 1);
  id[nq] = id[q];
  memcpy(nxt[nq], nxt[q], sizeof nxt[q]);
  par[nq] = par[q], par[q] = par[cur] = nq;
  while (p && nxt[p][t] == q) nxt[p][t] = nq, p = par[p];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> s + 1;
  init();
  has['('] = 0, has[')'] = 1;
  for (int i = 1; i <= n; ++i) {
    insert(s[i]);
    id[last] = i;
  }
  for (int i = n; i >= 1; --i) {
    sum[i] = sum[i + 1] + (s[i] == '(' ? -1 : 1);
    pi[sum[i] + n].push_back(i);
  }
  for (int i = 0; i <= 2 * n; ++i) {
    reverse(pi[i].begin(), pi[i].end());
  }
  sum[0] = -inf;
  for (int i = 1; i <= n + 1; ++i) {
    while (sum[i] <= sum[stk[top]]) --top;
    li[i] = stk[top];
    stk[++top] = i;
  }
  long long ret = 0;
  for (int i = 2; i <= cnt; ++i) {
    int rm = id[i], l = rm - len[i] + 1, r = rm - len[par[i]];
    l = max(l, li[rm + 1]);
    if (l > r) continue;
    int val = sum[rm + 1] + n, m = ((int)pi[val].size());
    int p1 =
        lower_bound(pi[val].begin(), pi[val].end(), l) - pi[val].begin() + 1;
    int p2 =
        upper_bound(pi[val].begin(), pi[val].end(), r) - pi[val].begin() + 1;
    if (p1 >= p2) continue;
    ret += p2 - p1;
  }
  cout << ret << "\n";
  return 0;
}
