#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &ret) {
  T x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  ret = x * f;
}
const int N = 500010, lgN = 20;
struct Tree {
  int sgt[N * 4];
  void build(int i, int b, int e, int *w) {
    if (b == e) {
      sgt[i] = w[b];
      return;
    }
    int mid = (b + e) >> 1, lc = i << 1, rc = lc | 1;
    build(lc, b, mid, w);
    build(rc, mid + 1, e, w);
    sgt[i] = min(sgt[lc], sgt[rc]);
  }
  int query(int i, int b, int e, int l, int r) {
    if (r < b || e < l) return (long long)0x3f3f3f3f;
    if (l <= b && e <= r) return sgt[i];
    int mid = (b + e) >> 1, lc = i << 1, rc = lc | 1;
    return min(query(lc, b, mid, l, r), query(rc, mid + 1, e, l, r));
  }
} t;
struct SA {
  int rk[N], sa[N], ht[N];
  void radix(int *str, int *a, int *b, int n, int m) {
    static int cnt[N];
    fill(cnt, cnt + m + 1, 0);
    for (long long i = (0); i <= (n - 1); i++) cnt[str[a[i]]]++;
    for (long long i = (1); i <= (m); i++) cnt[i] += cnt[i - 1];
    for (long long i = (n - 1); i >= (0); i--) b[--cnt[str[a[i]]]] = a[i];
  }
  void Sa(int *str, int n, int m) {
    static int a[N], b[N];
    for (long long i = (0); i <= (n - 1); i++) rk[i] = i;
    radix(str, rk, sa, n, m);
    rk[sa[0]] = 0;
    for (long long i = (1); i <= (n - 1); i++)
      rk[sa[i]] = rk[sa[i - 1]] + (str[sa[i]] != str[sa[i - 1]]);
    for (int i = 0; (1 << i) < n; i++) {
      for (long long j = (0); j <= (n - 1); j++) {
        a[j] = rk[j] + 1;
        b[j] = (j + (1 << i) >= n) ? 0 : (rk[j + (1 << i)] + 1);
        sa[j] = j;
      }
      radix(b, sa, rk, n, n);
      radix(a, rk, sa, n, n);
      rk[sa[0]] = 0;
      for (long long j = (1); j <= (n - 1); j++)
        rk[sa[j]] = rk[sa[j - 1]] +
                    (a[sa[j - 1]] != a[sa[j]] || b[sa[j - 1]] != b[sa[j]]);
      if (rk[sa[n - 1]] == n - 1) break;
    }
  }
  void calc_ht(int *str, int n) {
    int k = 0;
    for (long long i = (0); i <= (n - 1); i++) {
      if (rk[i] == 0)
        k = 0;
      else {
        if (k > 0) k--;
        int j = sa[rk[i] - 1];
        while (i + k < n && j + k < n && str[i + k] == str[j + k]) k++;
      }
      ht[rk[i]] = k;
    }
  }
} sa;
int n, lgn;
int pref[N], nxt[N][lgN];
char s[N];
void init() {
  static int lst[2 * N];
  memset(lst, -1, sizeof(lst));
  lst[0 + N] = 0;
  for (long long i = (1); i <= (n); i++) {
    int l = lst[pref[i] + N];
    if (l != -1 && t.query(1, 0, n, l, i) >= pref[i]) nxt[l][0] = i;
    lst[pref[i] + N] = i;
  }
  for (long long j = (1); j <= (lgn); j++)
    for (long long i = (0); i <= (n); i++)
      if (nxt[i][j - 1]) nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
}
long long solv(int l, int r) {
  l++;
  r++;
  if (r < l) return 0;
  long long ans = 0;
  int i = l - 1;
  for (long long j = (lgn); j >= (0); j--)
    if (nxt[i][j] && nxt[i][j] <= r) i = nxt[i][j], ans += 1 << j;
  return ans;
}
void solv() {
  static int str[N];
  for (long long i = (0); i <= (n - 1); i++) str[i] = s[i] == '(' ? 0 : 1;
  sa.Sa(str, n, 2);
  sa.calc_ht(str, n);
  long long ans = 0;
  for (long long i = (0); i <= (n - 1); i++)
    ans += solv(i, n - 1) - solv(i, i + sa.ht[sa.rk[i]] - 1);
  cout << ans;
}
int main() {
  read(n);
  lgn = ceil(log2(n));
  scanf("%s", s);
  for (long long i = (1); i <= (n); i++)
    pref[i] = pref[i - 1] + (s[i - 1] == '(' ? 1 : -1);
  t.build(1, 0, n, pref);
  init();
  solv();
}
