#include <bits/stdc++.h>
using namespace std;
const int Max = 5e5 + 10;
const int LOG = 20;
int n;
string s;
int mn[Max * 4];
pair<int, int> seg[Max * 4];
int lazy[Max * 4];
pair<int, int> merge(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first) return make_pair(a.first, a.second + b.second);
  return min(a, b);
}
void build(int l, int r, int idx = 1) {
  if (r - l == 1) {
    seg[idx] = make_pair(0, 1);
    mn[idx] = 0;
    return;
  }
  int mid = (l + r) / 2;
  build(l, mid, idx * 2);
  build(mid, r, idx * 2 + 1);
  seg[idx] = merge(seg[idx * 2], seg[idx * 2 + 1]);
  mn[idx] = min(mn[idx * 2], mn[idx * 2 + 1]);
}
void put(int x, int idx) {
  mn[idx] += x;
  seg[idx] = make_pair(seg[idx].first + x, seg[idx].second);
  lazy[idx] += x;
}
void shift(int idx) {
  if (lazy[idx] == 0) return;
  put(lazy[idx], idx * 2);
  put(lazy[idx], idx * 2 + 1);
  lazy[idx] = 0;
}
void upd(int l, int r, int x, int tl = 0, int tr = n, int idx = 1) {
  if (l <= tl && tr <= r) {
    put(x, idx);
    return;
  }
  if (r <= tl || tr <= l) return;
  int mid = (tl + tr) / 2;
  shift(idx);
  upd(l, r, x, tl, mid, idx * 2);
  upd(l, r, x, mid, tr, idx * 2 + 1);
  mn[idx] = min(mn[idx * 2], mn[idx * 2 + 1]);
  seg[idx] = merge(seg[idx * 2], seg[idx * 2 + 1]);
}
int get(int tl = 0, int tr = n, int idx = 1) {
  if (mn[idx] >= 0) return n;
  if (tr - tl == 1) return tl;
  int mid = (tl + tr) / 2;
  shift(idx);
  if (mn[idx * 2] < 0) return get(tl, mid, idx * 2);
  return get(mid, tr, idx * 2 + 1);
}
pair<int, int> ans(int l, int r, int tl = 0, int tr = n, int idx = 1) {
  if (l <= tl && tr <= r) return seg[idx];
  if (r <= tl || tr <= l) return make_pair(1000000000, 0);
  int mid = (tl + tr) / 2;
  shift(idx);
  return merge(ans(l, r, tl, mid, idx * 2), ans(l, r, mid, tr, idx * 2 + 1));
}
int rnk[LOG][Max];
int pos[Max];
pair<pair<int, int>, int> sorter[Max];
int lcp[Max];
void build() {
  for (int i = 0; i < n; i++) rnk[0][i] = s[i] - '(';
  for (int lg = 1, sz = 1; lg < LOG; lg++, sz <<= 1) {
    for (int i = 0; i < n; i++)
      sorter[i] = make_pair(
          make_pair(rnk[lg - 1][i], i + sz < n ? rnk[lg - 1][i + sz] : -1), i);
    sort(sorter, sorter + n);
    for (int i = 0, j = 0; i < n; i++)
      rnk[lg][sorter[i].second] = j =
          j + (i > 0 && sorter[i].first != sorter[i - 1].first);
  }
  for (int i = 0; i < n; i++) pos[rnk[LOG - 1][i]] = i;
  for (int lg = LOG; lg--;)
    for (int i = 0; i < n - 1; i++)
      if (max(pos[i], pos[i + 1]) + lcp[i] < n &&
          rnk[lg][pos[i] + lcp[i]] == rnk[lg][pos[i + 1] + lcp[i]])
        lcp[i] |= (1 << lg);
  build(0, n);
  for (int i = 0; i < n; i++) upd(i, n, s[i] == '(' ? 1 : -1);
}
int main() {
  cin >> n >> s;
  build();
  long long ANS = 0;
  for (int i = 0; i < n; i++) {
    if (i > 0) upd(i - 1, n, s[i - 1] == '(' ? -1 : 1);
    int x = get();
    pair<int, int> res;
    if (rnk[LOG - 1][i] > 0)
      res = ans(lcp[rnk[LOG - 1][i] - 1] + i, x);
    else
      res = ans(i, x);
    if (res.first == 0) ANS += res.second;
  }
  cout << '\n';
  cout << ANS << '\n';
}
