#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int ch[N][2], fa[N], len[N], tot = 1, lst = 1, n, pos[N];
char s[N];
vector<int> G[N];
void insert(int c, int ps) {
  int p = lst, np = lst = ++tot;
  pos[np] = ps;
  len[np] = len[p] + 1;
  for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
  if (!p)
    fa[np] = 1;
  else {
    int q = ch[p][c];
    if (len[q] == len[p] + 1)
      fa[np] = q;
    else {
      int nq = ++tot;
      len[nq] = len[p] + 1;
      memcpy(ch[nq], ch[q], sizeof *ch);
      fa[nq] = fa[q], fa[q] = fa[np] = nq;
      for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
    }
  }
}
void DFS(int u) {
  for (int v : G[u]) DFS(v), pos[u] = max(pos[u], pos[v]);
}
vector<pair<int, int> > vc[N];
struct info {
  int mn, ct;
  inline info operator+(const info& rhs) const {
    if (mn < rhs.mn) return *this;
    if (mn > rhs.mn) return rhs;
    return (info){mn, ct + rhs.ct};
  }
} d[N * 4];
int tag[N * 4];
inline void pushdown(int o) {
  if (int& x = tag[o]) {
    tag[o << 1] += x, tag[o << 1 | 1] += x;
    d[o << 1].mn += x, d[o << 1 | 1].mn += x;
    x = 0;
  }
}
void build(int l, int r, int o) {
  if (l == r) {
    d[o] = (info){0, 1};
  } else {
    const int mid = (l + r) / 2;
    build(l, mid, o << 1);
    build(mid + 1, r, o << 1 | 1);
    d[o] = d[o << 1] + d[o << 1 | 1];
  }
}
void modify(int l, int r, int o, const int& L, const int& R, const int& v) {
  if (L <= l && r <= R) {
    d[o].mn += v;
    tag[o] += v;
  } else {
    pushdown(o);
    const int mid = (l + r) / 2;
    if (L <= mid) modify(l, mid, o << 1, L, R, v);
    if (mid < R) modify(mid + 1, r, o << 1 | 1, L, R, v);
    d[o] = d[o << 1] + d[o << 1 | 1];
  }
}
int find(int l, int r, int o) {
  if (l == r)
    return d[o].mn < 0 ? l : 0;
  else {
    pushdown(o);
    const int mid = (l + r) / 2;
    if (d[o << 1 | 1].mn < 0) return find(mid + 1, r, o << 1 | 1);
    return find(l, mid, o << 1);
  }
}
info query(int l, int r, int o, const int& L, const int& R) {
  if (L <= l && r <= R) return d[o];
  pushdown(o);
  const int mid = (l + r) / 2;
  if (L <= mid && mid < R)
    return query(l, mid, o << 1, L, R) + query(mid + 1, r, o << 1 | 1, L, R);
  if (L <= mid) return query(l, mid, o << 1, L, R);
  return query(mid + 1, r, o << 1 | 1, L, R);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> (s + 1);
  for (int i = 1; i <= n; ++i) s[i] = s[i] == '(', insert(s[i], i);
  for (int i = 2; i <= tot; ++i) G[fa[i]].emplace_back(i);
  DFS(1);
  for (int i = 1; i <= tot; ++i) {
    int x = pos[i];
    int l = x - len[i] + 1, r = x - len[fa[i]];
    vc[x].emplace_back(l, r);
  }
  build(1, n, 1);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    modify(1, n, 1, 1, i, s[i] ? -1 : 1);
    int it = find(1, n, 1) + 1;
    if (it > i) continue;
    for (auto g : vc[i]) {
      int l = g.first, r = g.second;
      l = max(l, it);
      if (l <= r) {
        auto k = query(1, n, 1, l, r);
        if (k.mn == 0) ans += k.ct;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
