#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
  gn(first);
  gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
  gn(first);
  gn(second);
  gn(z);
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T>
inline void printsp(T first) {
  print(first);
  putchar(' ');
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int cnt[500050], mn[500050][22], n;
char s[500050];
int H[500050], Rank[500050], sa[500050];
vector<int> vec[500050 << 1];
pair<pair<int, int>, int> pos[500050];
void Height() {
  int h = 0;
  for (int i = 0; i < n; i++) {
    if (Rank[i] == 1) {
      H[Rank[i]] = 0;
      continue;
    }
    h = max(0, h - 1);
    for (int j = sa[Rank[i] - 1]; s[i + h] == s[j + h]; h++)
      ;
    H[Rank[i]] = h;
  }
}
void buildSA() {
  for (int i = 0; i <= n; i++) Rank[i] = s[i];
  for (int j = 0; j < 19; j++) {
    for (int i = 0; i < n; i++)
      pos[i] = pair<pair<int, int>, int>(
          pair<int, int>(Rank[i], Rank[min(n, i + (1 << j))]), i);
    sort(pos, pos + n);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      Rank[pos[i].second] = cnt + 1;
      cnt += (pos[i].first != pos[i + 1].first);
    }
    if (cnt == n) break;
  }
  for (int i = 0; i < n; i++) sa[Rank[i]] = i;
  Height();
}
int calc(int l, int r) {
  int k = 31 - (__builtin_clz(r - l));
  return min(mn[l][k], mn[r - (1 << k)][k]);
}
int main() {
  cin >> n;
  scanf("%s", s);
  buildSA();
  int d = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      d++;
    else
      d--;
    vec[d + 500050].push_back(i);
    cnt[i] = d;
  }
  cnt[n] = 0x3f3f3f3f;
  for (int i = 0; i < 500050 << 1; i++)
    if (vec[i].size()) vec[i].push_back(n);
  for (int i = 0; i <= n; i++) mn[i][0] = cnt[i];
  for (int k = 1; k < 22; k++)
    for (int i = 0; i + (1 << k) <= n + 1; i++)
      mn[i][k] = min(mn[i][k - 1], mn[i + (1 << k - 1)][k - 1]);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int u = sa[i];
    int d = u ? cnt[u - 1] : 0;
    int sz = vec[d + 500050].size();
    if (sz == 0) continue;
    int v = upper_bound(vec[d + 500050].begin(), vec[d + 500050].end(),
                        max(u, u + H[i] - 1)) -
            vec[d + 500050].begin();
    if (v == sz - 1) continue;
    if (calc(u, vec[d + 500050][v] + 1) < d) continue;
    int st = v;
    int ed = sz - 1;
    while (ed - st > 1) {
      int md = st + ed >> 1;
      int k = vec[d + 500050][md];
      if (calc(u, k + 1) >= d)
        st = md;
      else
        ed = md;
    }
    ans += (st - v + 1);
  }
  cout << ans << endl;
  return 0;
}
