#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int OFT = 5e5 + 5;
const int B = 13131;
const int M1 = 1e9 + 7;
const int M2 = 1e9 + 9;
struct SA {
  bool _t[N * 2];
  int _s[N * 2], _sa[N * 2], _c[N * 2], x[N], _p[N], _q[N * 2], hei[N], r[N];
  int operator[](int i) { return _sa[i]; }
  void build(int *s, int n, int m) {
    memcpy(_s, s, sizeof(int) * n);
    sais(_s, _sa, _p, _q, _t, _c, n, m);
    mkhei(n);
  }
  void mkhei(int n) {
    for (int i = 0; i < int(n); i++) r[_sa[i]] = i;
    hei[0] = 0;
    for (int i = 0; i < int(n); i++)
      if (r[i]) {
        int ans = i > 0 ? max(hei[r[i - 1]] - 1, 0) : 0;
        while (_s[i + ans] == _s[_sa[r[i] - 1] + ans]) ans++;
        hei[r[i]] = ans;
      }
  }
  void sais(int *s, int *sa, int *p, int *q, bool *t, int *c, int n, int z) {
    bool uniq = t[n - 1] = true, neq;
    int nn = 0, nmxz = -1, *nsa = sa + n, *ns = s + n, lst = -1;
    memset((c), 0, z * sizeof(*(c)));
    for (int i = 0; i < int(n); i++) uniq &= ++c[s[i]] < 2;
    for (int i = 0; i < int(z - 1); i++) c[i + 1] += c[i];
    if (uniq) {
      for (int i = 0; i < int(n); i++) sa[--c[s[i]]] = i;
      return;
    }
    for (int i = n - 2; i >= 0; i--)
      t[i] = (s[i] == s[i + 1] ? t[i + 1] : s[i] < s[i + 1]);
    memset((sa), 0, n * sizeof(*(sa)));
    memcpy(x, c, sizeof(int) * z);
    for (int i = (1); i <= int(n - 1); i++)
      if (t[i] && !t[i - 1]) sa[--x[s[i]]] = p[q[i] = nn++] = i;
    memcpy(x + 1, c, sizeof(int) * (z - 1));
    for (int i = 0; i < int(n); i++)
      if (sa[i] && !t[sa[i] - 1]) sa[x[s[sa[i] - 1]]++] = sa[i] - 1;
    memcpy(x, c, sizeof(int) * z);
    for (int i = n - 1; i >= 0; i--)
      if (sa[i] && t[sa[i] - 1]) sa[--x[s[sa[i] - 1]]] = sa[i] - 1;
    ;
    for (int i = 0; i < int(n); i++)
      if (sa[i] && t[sa[i]] && !t[sa[i] - 1]) {
        neq = lst < 0 || memcmp(s + sa[i], s + lst,
                                (p[q[sa[i]] + 1] - sa[i]) * sizeof(int));
        ns[q[lst = sa[i]]] = nmxz += neq;
      }
    sais(ns, nsa, p + nn, q + n, t + n, c + z, nn, nmxz + 1);
    memset((sa), 0, n * sizeof(*(sa)));
    memcpy(x, c, sizeof(int) * z);
    for (int i = nn - 1; i >= 0; i--) sa[--x[s[p[nsa[i]]]]] = p[nsa[i]];
    memcpy(x + 1, c, sizeof(int) * (z - 1));
    for (int i = 0; i < int(n); i++)
      if (sa[i] && !t[sa[i] - 1]) sa[x[s[sa[i] - 1]]++] = sa[i] - 1;
    memcpy(x, c, sizeof(int) * z);
    for (int i = n - 1; i >= 0; i--)
      if (sa[i] && t[sa[i] - 1]) sa[--x[s[sa[i] - 1]]] = sa[i] - 1;
    ;
  }
} sa;
int H[N], SA[N];
void suffix_array(int *ip, int len) {
  ip[len++] = 0;
  sa.build(ip, len, 128);
  for (int i = 0; i < len; i++) {
    H[i] = sa.hei[i + 1];
    SA[i] = sa._sa[i + 1];
  }
}
int n;
char ip[N];
int _s[N], *s = (_s + 1);
vector<int> _pos[N], *pos = (_pos + OFT);
int _r[N], *r = (_r + 1);
int rgt_most[N];
int main() {
  scanf("%d%s", &n, ip);
  pos[0].push_back(-1);
  for (int i = 0; i < n; ++i) {
    s[i] = s[i - 1];
    if (ip[i] == ')')
      s[i]--;
    else
      s[i]++;
    pos[s[i]].push_back(i);
  }
  {
    vector<int> stk;
    for (int i = n - 1; i >= -1; --i) {
      while (stk.size() && s[i] <= s[stk.back()]) stk.pop_back();
      if (stk.size())
        r[i] = stk.back() - 1;
      else
        r[i] = n - 1;
      stk.push_back(i);
    }
    for (int i = 0; i < n; ++i) {
      rgt_most[i] = 0;
      vector<int> &xs = pos[s[i - 1]];
      int j = upper_bound(xs.begin(), xs.end(), r[i - 1]) - xs.begin();
      --j;
      if (j >= 0) {
        rgt_most[i] = xs[j] - i + 1;
      }
    }
  }
  {
    static int buf[N];
    for (int i = 0; i < n; ++i) buf[i] = ip[i];
    suffix_array(buf, n);
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int id = SA[i];
    int plen = 0;
    if (i) {
      plen = min(H[i], rgt_most[SA[i - 1]]);
    }
    int lb = id + plen;
    int rb = id + rgt_most[id] - 1;
    vector<int> &xs = pos[s[id - 1]];
    int cnt = upper_bound(xs.begin(), xs.end(), rb) -
              lower_bound(xs.begin(), xs.end(), lb);
    ans += cnt;
  }
  cout << ans << endl;
}
