#include <bits/stdc++.h>
using namespace std;
struct pt {
  long double first, second;
  pt() {}
  pt(long double first, long double second) : first(first), second(second) {}
  pt operator+(const pt &r) const {
    return pt(first + r.first, second + r.second);
  }
  pt operator-(const pt &r) const {
    return pt(first - r.first, second - r.second);
  }
  pt operator*(const long double &r) const { return pt(first * r, second * r); }
};
long double dot(const pt &l, const pt &r) {
  return l.first * r.first + l.second * r.second;
}
long double cross(const pt &l, const pt &r) {
  return l.first * r.second - l.second * r.first;
}
int nxt() {
  int first;
  scanf("%d", &first);
  return first;
}
const int N = 500 * 1000 + 10;
char s[N];
int n;
int bal[N];
int logTable[N];
int rmq[20][N];
int m;
vector<int> pos[N + N];
int getMin(int i, int j) {
  int k = logTable[j - i];
  int first = rmq[k][i];
  int second = rmq[k][j - (1 << k) + 1];
  return min(first, second);
}
int sa[N];
int ra[N];
int lcp[N];
void init() {
  int s1[n];
  int c[n];
  int cl[n];
  int cnt[n];
  for (int i = 0; i < n; ++i) {
    cl[i] = s[i];
    sa[i] = n - i - 1;
  }
  stable_sort(sa, sa + n, [&](int l, int r) { return s[l] < s[r]; });
  for (int len = 1; len < n; len <<= 1) {
    memcpy(c, cl, n * sizeof(int));
    memcpy(s1, sa, n * sizeof(int));
    for (int i = 0; i < n; ++i) cnt[i] = i;
    int len2 = len >> 1;
    for (int i = 0; i < n; ++i) {
      if (i > 0 && c[sa[i]] == c[sa[i - 1]] && sa[i - 1] + len < n &&
          c[sa[i] + len2] == c[sa[i - 1] + len2]) {
        cl[sa[i]] = cl[sa[i - 1]];
      } else {
        cl[sa[i]] = i;
      }
    }
    for (int i = 0; i < n; ++i) {
      int s2 = s1[i] - len;
      if (s2 >= 0) {
        sa[cnt[cl[s2]]++] = s2;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    ra[sa[i]] = i;
  }
  for (int i = 0, h = 0; i < n; ++i) {
    if (ra[i] < n - 1) {
      for (int j = sa[ra[i] + 1]; max(i, j) + h < n && s[i + h] == s[j + h];
           ++h)
        ;
      lcp[ra[i] + 1] = h;
      h = max(0, h - 1);
    }
  }
}
int get(int b, int l, int r) {
  b += N;
  int psl = lower_bound((pos[b]).begin(), (pos[b]).end(), l) - pos[b].begin();
  int psr =
      upper_bound((pos[b]).begin(), (pos[b]).end(), r) - pos[b].begin() - 1;
  if (psl > psr) return 0;
  return psr - psl + 1;
}
void solve() {
  n = nxt();
  scanf("%s", s);
  bal[0] = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '(') {
      bal[i + 1] = bal[i] + 1;
    } else {
      bal[i + 1] = bal[i] - 1;
    }
  }
  init();
  m = n + 1;
  for (int i = 0; i < m; ++i) {
    pos[bal[i] + N].push_back(i);
  }
  for (int i = 2; i <= m; ++i) {
    logTable[i] = logTable[i >> 1] + 1;
  }
  for (int i = 0; i < m; ++i) rmq[0][i] = bal[i];
  for (int k = 1; (1 << k) < m; ++k) {
    for (int i = 0; i + (1 << k) <= m; i++) {
      rmq[k][i] = min(rmq[k - 1][i], rmq[k - 1][i + (1 << (k - 1))]);
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int v = sa[i];
    int l = v, r = m;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (getMin(v, mid) >= bal[v]) {
        l = mid;
      } else {
        r = mid - 1;
      }
    }
    int le = v + 1;
    int ri = l;
    le = max(le, v + lcp[i] + 1);
    ans += get(bal[v], le, ri);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  for (int i = 0; i < (int)(t); i++) {
    solve();
  }
  return 0;
}
