#include <bits/stdc++.h>
using namespace std;
const int maxn = 500050;
const int maxk = 1000050;
const int lim = 500000;
vector<int> in[maxn], out[maxn];
int st[maxn][20];
int t[maxn], x[maxn], y[maxn], sa[maxn], rnk[maxn], height[maxn];
int sum[maxn], tot[maxk];
char s[maxn];
int n, m;
void get_sa() {
  for (int i = 1; i <= m; i++) t[i] = 0;
  for (int i = 1; i <= n; i++) t[x[i] = s[i]]++;
  for (int i = 2; i <= m; i++) t[i] += t[i - 1];
  for (int i = n; i >= 1; i--) sa[t[x[i]]--] = i;
  for (int k = 1; k <= n; k <<= 1) {
    int num = 0;
    for (int i = n - k + 1; i <= n; i++) y[++num] = i;
    for (int i = 1; i <= n; i++) {
      if (sa[i] > k) y[++num] = sa[i] - k;
    }
    for (int i = 1; i <= m; i++) t[i] = 0;
    for (int i = 1; i <= n; i++) t[x[i]]++;
    for (int i = 2; i <= m; i++) t[i] += t[i - 1];
    for (int i = n; i >= 1; i--) sa[t[x[y[i]]]--] = y[i];
    swap(x, y);
    x[sa[1]] = 1;
    num = 1;
    for (int i = 2; i <= n; i++) {
      x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])
                     ? num
                     : ++num;
    }
    if (num == n) break;
    m = num;
  }
}
void get_height() {
  for (int i = 1; i <= n; i++) rnk[sa[i]] = i;
  int k = 0;
  for (int i = 1; i <= n; i++) {
    if (rnk[i] == 1) continue;
    if (k) k--;
    int j = sa[rnk[i] - 1];
    while (j + k <= n && i + k <= n && s[i + k] == s[j + k]) k++;
    height[rnk[i]] = k;
  }
}
void get_st() {
  for (int i = 1; i <= n; i++) st[i][0] = sum[i];
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int rmq(int l, int r) {
  int k = 0, len = r - l + 1;
  while (1 << (k + 1) <= len) k++;
  return min(st[l][k], st[r - (1 << k) + 1][k]);
}
void solve() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + ((s[i] == '(') ? 1 : -1);
  m = 128;
  get_sa();
  get_height();
  get_st();
  for (int i = 1; i <= n; i++) {
    int pos = sa[i] + height[i];
    int l = pos, r = n;
    while (l <= r) {
      int mid = l + r >> 1;
      if (rmq(sa[i], mid) >= sum[sa[i] - 1])
        l = mid + 1;
      else
        r = mid - 1;
    }
    in[pos - 1].push_back(sum[sa[i] - 1]);
    out[r].push_back(sum[sa[i] - 1]);
  }
  long long res = 0;
  for (int i = 0; i <= n; i++) {
    tot[sum[i] + lim]++;
    for (int j = 0, sz = in[i].size(); j < sz; j++) {
      int num = in[i][j];
      res -= tot[num + lim];
    }
    for (int j = 0, sz = out[i].size(); j < sz; j++) {
      int num = out[i][j];
      res += tot[num + lim];
    }
  }
  printf("%lld\n", res);
}
int main() {
  solve();
  return 0;
}
