#include <bits/stdc++.h>
using namespace std;
const int N = 500000;
const int M = 510000;
int n;
char s[N + 7];
int rnk[N + 7];
int suf[N + 7];
int lcp[N + 7];
pair<long long, int> kmrpom[N + 7];
int inff = 1e9;
int sum[N + 7];
int ne[N + 7];
void licz_tablice() {
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '(')
      rnk[i] = 1;
    else
      rnk[i] = 2;
  }
  for (int j = 0; (1 << j) <= n; ++j) {
    int l = (1 << j);
    for (int i = 1; i <= n; ++i) {
      kmrpom[i].first = (long long)rnk[i] * inff;
      if (i + l <= n) kmrpom[i].first += rnk[i + l];
      kmrpom[i].second = i;
    }
    sort(kmrpom + 1, kmrpom + n + 1);
    int zz = 1;
    for (int i = 1; i <= n; ++i) {
      rnk[kmrpom[i].second] = zz;
      if (kmrpom[i].first != kmrpom[i + 1].first) zz++;
    }
  }
  for (int i = 1; i <= n; ++i) suf[rnk[i]] = i;
  int zz = 0;
  for (int i = 1; i <= n; ++i) {
    int u = rnk[i];
    if (u < n)
      while (s[suf[u] + zz] == s[suf[u + 1] + zz]) zz++;
    lcp[u] = zz;
    zz = max(0, zz - 1);
  }
}
int D[2 * M + 7];
void insert(int x, int w) {
  for (; x <= 2 * M; x += (x & -x)) D[x] = min(D[x], w);
}
int query(int x) {
  int res = n + 1;
  for (; x > 0; x -= (x & -x)) res = min(res, D[x]);
  return res;
}
struct zap {
  int q;
  int a, b;
  zap(int Q, int A, int B) : q(Q), a(A), b(B) {}
};
vector<int> wor[2 * M + 7];
int D2[N + 7];
void insert2(int x, int w) {
  for (; x <= N; x += (x & -x)) D2[x] += w;
}
int query2(int x) {
  int res = 0;
  for (; x > 0; x -= (x & -x)) res += D2[x];
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  cin >> (s + 1);
  licz_tablice();
  sum[0] = M;
  for (int i = 1; i <= n; ++i) {
    sum[i] = sum[i - 1];
    if (s[i] == '(')
      sum[i]++;
    else
      sum[i]--;
    wor[sum[i]].push_back(i);
  }
  for (int i = 0; i <= 2 * M; ++i) D[i] = n + 1;
  for (int i = n; i >= 1; --i) {
    insert(sum[i], i);
    ne[i] = query(sum[i - 1] - 1);
  }
  vector<zap> T;
  for (int i = 1; i <= n; ++i) {
    if (suf[i] + lcp[i] <= ne[suf[i]] - 1)
      T.push_back(zap(sum[suf[i] - 1], suf[i] + lcp[i], ne[suf[i]] - 1));
  }
  sort(T.begin(), T.end(),
       [](const zap &i, const zap &j) -> bool { return i.q < j.q; });
  long long wynik = 0;
  int akt = 0;
  for (auto u : T) {
    if (u.q != akt) {
      for (auto v : wor[akt]) insert2(v, -1);
      akt = u.q;
      for (auto v : wor[akt]) insert2(v, 1);
    }
    wynik += query2(u.b) - query2(u.a - 1);
  }
  cout << wynik << endl;
  return 0;
}
