#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)5e5 + 3;
const int infint = (int)1e9;
string s;
int n, gap, sa[MAXN], pos[MAXN], tmp[MAXN], lcp[MAXN], pre[MAXN], nxt[MAXN];
map<int, vector<int> > oc;
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() {
  for (int i = 0; i < n; i++) sa[i] = i, pos[i] = s[i];
  for (gap = 1;; gap *= 2) {
    sort(sa, sa + n, sufCmp);
    for (int i = 0; i < n - 1; i++)
      tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
    for (int i = 0; i < 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--;
    }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> s;
  buildSA();
  buildLCP();
  for (int i = 1; i <= s.size(); i++)
    pre[i] = pre[i - 1] + (s[i - 1] == '(' ? 1 : -1);
  stack<int> S;
  S.push(s.size());
  for (int i = s.size() - 1; i >= 0; i--) {
    while (S.size() && pre[i] <= pre[S.top()]) S.pop();
    if (S.empty())
      nxt[i] = s.size();
    else
      nxt[i] = S.top() - 1;
    S.push(i);
  }
  memset(pre, 0, sizeof pre);
  for (int i = 0; i < s.size(); i++) {
    pre[i] = (s[i] == '(' ? 1 : -1);
    if (i) pre[i] += pre[i - 1];
  }
  for (int i = 0; i <= s.size(); i++) oc[pre[i]].push_back(i);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int idx = sa[i], L, R;
    R = nxt[idx] - 1;
    if (i == 0)
      L = idx;
    else
      L = idx + lcp[i - 1];
    int pic;
    if (idx == 0)
      pic = 0;
    else
      pic = pre[idx - 1];
    L = lower_bound(oc[pic].begin(), oc[pic].end(), L) - oc[pic].begin();
    R = upper_bound(oc[pic].begin(), oc[pic].end(), R) - oc[pic].begin() - 1;
    if (R < L) continue;
    ans += R - L + 1;
  }
  cout << ans;
}
