#include <bits/stdc++.h>
using namespace std;
const int mn = 5e5;
int n;
char a[mn + 3];
struct SAM {
  int to[2], fa, len;
} tr[2 * mn + 3];
int tot = 1, all = 1;
void Add(int c) {
  int p = all;
  all = ++tot;
  tr[all].len = tr[p].len + 1;
  while (p && !tr[p].to[c]) {
    tr[p].to[c] = all;
    p = tr[p].fa;
  }
  if (!p)
    tr[all].fa = 1;
  else {
    int q = tr[p].to[c];
    if (tr[q].len == tr[p].len + 1)
      tr[all].fa = q;
    else {
      int newq = ++tot;
      for (int cc = 0; cc <= 1; cc++) tr[newq].to[cc] = tr[q].to[cc];
      tr[newq].len = tr[p].len + 1;
      tr[newq].fa = tr[q].fa;
      tr[q].fa = tr[all].fa = newq;
      while (p && tr[p].to[c] == q) {
        tr[p].to[c] = newq;
        p = tr[p].fa;
      }
    }
  }
}
int lto[mn + 3];
vector<int> b[mn + 3];
int atb[mn + 3], tb = 0;
int main() {
  cin >> n;
  scanf("%s", a + 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    Add(a[i] == '(' ? 0 : 1);
    if (a[i] == ')') {
      int w = i - 1;
      while (w > 0 && a[w] == ')') w = lto[w] - 1;
      if (a[w] == '(') {
        if (lto[w - 1] > 0) {
          lto[i] = lto[w - 1];
          atb[i] = atb[w - 1];
          b[atb[i]].push_back(w);
        } else {
          lto[i] = w;
          atb[i] = ++tb;
          b[atb[i]].push_back(w);
        }
        ans += upper_bound(b[atb[i]].begin(), b[atb[i]].end(),
                           i - tr[tr[all].fa].len) -
               b[atb[i]].begin();
      }
    }
  }
  cout << ans;
  return 0;
}
