#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxn = 500006;
void radix(int *s, int *a, int *b, int n, int m) {
  static int c[mxn];
  fill(c, c + m + 1, 0);
  for (int i = n; i--;) ++c[s[a[i]]];
  partial_sum(c, c + m + 1, c);
  for (int i = n; i--;) b[--c[s[a[i]]]] = a[i];
}
void SA(int *s, int *sa, int *h, int n, int m) {
  static int a[mxn * 2], t[mxn];
  fill(a, a + n * 2, 0);
  copy(s, s + n, a);
  for (int len = 1; len / 2 < n; len *= 2) {
    auto b = a + len / 2;
    iota(sa, sa + n, 0);
    radix(b, sa, t, n, m);
    radix(a, t, sa, n, m);
    t[sa[0]] = (len < n);
    for (int i = 1; i < n; ++i)
      t[sa[i]] =
          t[sa[i - 1]] + (a[sa[i]] != a[sa[i - 1]] || b[sa[i]] != b[sa[i - 1]]);
    copy(t, t + n, a);
    m = n;
  }
  int k = 0;
  for (int i = 0; i < n; ++i) {
    if (k) --k;
    if (a[i])
      while (s[i + k] == s[sa[a[i] - 1] + k]) ++k;
    h[a[i]] = k;
  }
}
int n;
char s[mxn];
int a[mxn];
int sa[mxn];
int h[mxn];
int c[mxn];
vector<int> p[mxn];
map<int, int> fir;
int loc[mxn];
void EXEC() {
  cin >> n;
  scanf("%s", s);
  for (int i = 0; i < n; ++i) a[i] = s[i];
  SA(a, sa, h, n, 128);
  c[0] = 0;
  fir[0] = 0;
  loc[0] = 0;
  p[0].push_back(0);
  for (int i = 1; i <= n; ++i) {
    c[i] = c[i - 1] + (s[i - 1] == '(' ? +1 : -1);
    if (s[i - 1] == '(') {
      fir[c[i]] = i;
      loc[i] = fir[c[i]];
      p[loc[i]].push_back(i);
    } else {
      fir.erase(c[i] + 1);
      if (!fir.count(c[i])) fir[c[i]] = i;
      loc[i] = fir[c[i]];
      p[loc[i]].push_back(i);
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int L = sa[i];
    int Rl = sa[i] + h[i] + 1;
    auto &b = p[loc[L]];
    ans += b.end() - lower_bound(b.begin(), b.end(), Rl);
  }
  cout << ans << endl;
}
int main() {
  EXEC();
  return 0;
}
