#include <bits/stdc++.h>
using namespace std;
const long long N = 500010;
const long long LOG = 22;
int n, R[N][LOG], pw, p[N], lcp[N], ps[N], r[N];
string s;
vector<int> koj[N << 1];
bool cmp(int x, int y) {
  if (R[x][pw] != R[y][pw]) return R[x][pw] < R[y][pw];
  if (max(x, y) + (1 << pw) > n) return x > y;
  return R[x + (1 << pw)][pw] < R[y + (1 << pw)][pw];
}
int GetLcp(int x, int y) {
  long long res = 0;
  for (int i = LOG - 1; i >= 0 && x <= n && y <= n; i--) {
    if (R[x][i] == R[y][i]) {
      res ^= (1 << i), x += (1 << i), y += (1 << i);
    }
  }
  return res;
}
void SuffixArray() {
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    R[i][0] = s[i - 1];
  }
  for (pw; pw < LOG - 1; pw++) {
    sort(p + 1, p + n + 1, cmp);
    R[p[1]][pw + 1] = 1;
    for (int i = 2; i <= n; i++) {
      R[p[i]][pw + 1] = R[p[i - 1]][pw + 1] + cmp(p[i - 1], p[i]);
    }
  }
  for (int i = 1; i < n; i++) {
    lcp[i] = GetLcp(p[i], p[i + 1]);
  }
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  cin >> s;
  SuffixArray();
  for (int i = 1; i <= n; i++) {
    (s[i - 1] == ')' ? ps[i] = ps[i - 1] - 1 : ps[i] = ps[i - 1] + 1);
    koj[ps[i] + N].push_back(i);
  }
  for (int i = 1; i <= n; i++) koj[ps[i] + N].push_back(100000000);
  r[n + 1] = n + 1;
  ps[n + 1] = -10000000;
  for (int i = n; i >= 1; i--) {
    r[i] = i + 1;
    while (ps[r[i]] >= ps[i - 1]) r[i] = r[r[i]];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (s[p[i] - 1] == ')') break;
    int Now = p[i] + lcp[i - 1];
    if (r[p[i]] <= Now) continue;
    r[p[i]]--;
    long long K = int(upper_bound(koj[ps[p[i] - 1] + N].begin(),
                                  koj[ps[p[i] - 1] + N].end(), r[p[i]]) -
                      koj[ps[p[i] - 1] + N].begin()) -
                  1;
    long long K1 = int(upper_bound(koj[ps[p[i] - 1] + N].begin(),
                                   koj[ps[p[i] - 1] + N].end(), Now - 1) -
                       koj[ps[p[i] - 1] + N].begin()) -
                   1;
    ans += K - K1;
  }
  cout << ans;
}
