#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
const int N = 501000 * 2;
char s[N];
int n, rk[N], sa[N], ht[N], r[N], pro[N];
pair<pair<int, int>, int> pos[N];
vector<int> vec[N];
void buildSA(char *p) {
  for (int i = 0; i < n; i++) rk[i] = p[i];
  for (int j = 0; j < 19; j++) {
    for (int i = 0; i < n; i++)
      pos[i] = make_pair(make_pair(rk[i], rk[i + (1 << j)]), i);
    sort(pos, pos + n);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      rk[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[rk[i]] = i;
}
void height() {
  int k = 0;
  for (int i = 0; i < n; i++) {
    if (rk[i] == 1) continue;
    k = max(0, k - 1);
    for (int j = sa[rk[i] - 1]; s[i + k] == s[j + k]; k++)
      ;
    ht[rk[i]] = k;
  }
}
int main() {
  scanf("%d", &n);
  scanf("%s", s);
  buildSA(s);
  height();
  for (int i = 1; i < n + 1; i++) {
    r[sa[i]] = sa[i] + ht[i];
  }
  int cur = n;
  for (int i = 0; i < n; i++) {
    pro[i] = cur;
    if (s[i] == '(')
      cur++;
    else
      cur--;
  }
  pro[n] = cur;
  long long ret = 0;
  for (int i = n - 1; i >= 0; i--) {
    vec[pro[i + 1]].push_back(-i - 1);
    int pr = n;
    if (!vec[pro[i] - 1].empty()) pr = -vec[pro[i] - 1].back() - 1;
    if (pr <= r[i]) continue;
    ret -= lower_bound((vec[pro[i]]).begin(), (vec[pro[i]]).end(), -(pr + 1)) -
           lower_bound((vec[pro[i]]).begin(), (vec[pro[i]]).end(), -r[i]);
  }
  printf("%lld\n", ret);
}
