#include <bits/stdc++.h>
using namespace std;
int n, sum[500050];
long long ans;
char s[500050];
struct node {
  int fa, len, ch[2], f;
} tr[500050 << 2];
int las = 1, tot = 1;
inline int extend(int c, int id) {
  if (tr[tr[las].ch[c]].len == tr[las].len + 1) return tr[las].ch[c];
  int p = las, np = ++tot;
  tr[np].f = id;
  tr[np].len = tr[p].len + 1;
  for (; p && !tr[p].ch[c]; p = tr[p].fa) tr[p].ch[c] = np;
  if (!p) {
    tr[np].fa = 1;
    return np;
  }
  int q = tr[p].ch[c];
  if (tr[q].len == tr[p].len + 1) {
    tr[np].fa = q;
    return np;
  }
  int nq = ++tot;
  tr[nq] = tr[q];
  tr[nq].len = tr[p].len + 1;
  tr[q].fa = tr[np].fa = nq;
  for (; p && tr[p].ch[c] == q; p = tr[p].fa) tr[p].ch[c] = nq;
  if (tr[np].len == tr[nq].len) return nq;
  return np;
}
vector<pair<int, int> > ve[500050];
vector<int> v[500050 << 1];
int main() {
  cin >> n;
  scanf("%s", s + 1);
  for (int i = 1; i <= n; ++i) {
    sum[i] = max(sum[i - 1], 0);
    if (s[i] == '(')
      ++sum[i];
    else
      --sum[i];
    if (sum[i] < 0) {
      las = 1;
      continue;
    }
    las = extend(s[i] == '(' ? 0 : 1, i);
  }
  for (int i = 2; i <= tot; ++i)
    ve[tr[i].f].push_back(
        make_pair(tr[i].f - tr[i].len + 1, tr[i].f - tr[tr[i].fa].len));
  int ad = 0;
  v[0].push_back(0);
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '(')
      ++ad;
    else
      v[ad].clear(), --ad;
    if (ad < 0) {
      ad = 0;
      v[0].push_back(i);
      continue;
    }
    if (s[i] == '(') ve[i].clear();
    for (pair<int, int> p : ve[i]) {
      int l = p.first, r = p.second;
      if (l > r) continue;
      --l;
      --r;
      int t1 = lower_bound(v[ad].begin(), v[ad].end(), l) - v[ad].begin() - 1;
      int t2 = upper_bound(v[ad].begin(), v[ad].end(), r) - v[ad].begin() - 1;
      ans += t2 - t1;
    }
    if (s[i + 1] == '(') v[ad].push_back(i);
  }
  cout << ans << endl;
  return 0;
}
