#include <bits/stdc++.h>
using namespace std;
int sa[500007], pos[500007], tmp[500007];
int N;
int gap;
vector<int> v[500007];
int val[500007];
struct node {
  int count;
  node *left, *right;
  node(int count, node *left, node *right)
      : count(count), left(left), right(right) {}
  node *insert(int beg, int last, int w);
};
node *null = new node(0, NULL, NULL);
node *node::insert(int beg, int last, int w) {
  if (beg > w || last < w) return this;
  if (beg == last) return new node(this->count + 1, null, null);
  int mid = (beg + last) >> 1;
  return new node(this->count + 1, this->left->insert(beg, mid, w),
                  this->right->insert(mid + 1, last, w));
}
node *root[511111];
bool cmp(int i, int j) {
  if (pos[i] != pos[j]) return pos[i] < pos[j];
  i += gap;
  j += gap;
  return (i < N && j < N) ? pos[i] < pos[j] : i > j;
}
int query(node *a, int beg, int last, int p, int q) {
  if (a->count == 0) return 0;
  if (beg > q || last < p) return 0;
  if (p <= beg && q >= last) return a->count;
  int mid = (beg + last) / 2;
  return query(a->left, beg, mid, p, q) + query(a->right, mid + 1, last, p, q);
}
void build(char *s, int n) {
  N = n;
  for (int i = 0; i < N; i++) sa[i] = i, pos[i] = s[i];
  for (gap = 1;; gap *= 2) {
    sort(sa, sa + N, cmp);
    for (int i = 0; i < N - 1; i++) tmp[i + 1] = tmp[i] + cmp(sa[i], sa[i + 1]);
    for (int i = 0; i < N; i++) pos[sa[i]] = tmp[i];
    if (tmp[N - 1] == N - 1) break;
  }
}
void lcp(char *s, int *sa, int n, vector<int> &LCP) {
  LCP.resize(n, 0);
  vector<int> rankk(n, 0);
  for (int i = 0; i < n; i++) rankk[sa[i]] = i;
  for (int i = 0, k = 0; i < n; i++, k ? k-- : 0) {
    if (rankk[i] == n - 1) {
      k = 0;
      continue;
    }
    int j = sa[rankk[i] + 1];
    while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
    LCP[rankk[i]] = k;
  }
}
long long func(int n, char *a) {
  null->left = null->right = null;
  for (int i = 0; i <= n; i++) {
    root[i] = null;
    root[i]->left = null;
    root[i]->right = null;
  }
  build(a, n);
  vector<int> LCP;
  lcp(a, sa, n, LCP);
  stack<int> s;
  for (int i = 0; i < n; i++)
    if (a[i] == '(')
      s.push(i);
    else if (!s.empty()) {
      v[s.top()].push_back(i);
      s.pop();
    }
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--)
    if (v[i].size() != 0) {
      int temp = v[i][0];
      root[i] = root[temp + 1]->insert(0, n - 1, v[i][0]);
      val[i] = 1;
      val[i] += val[temp + 1];
    }
  for (int i = 0; i < n; i++) {
    ans += val[sa[i]];
    if (i == n - 1) continue;
    if (v[sa[i]].size() != 0) {
      int temp = query(root[sa[i]], 0, n - 1, 0, sa[i] + LCP[i] - 1);
      ans -= temp;
    }
  }
  return ans;
}
char a[500007];
int main() {
  int n;
  scanf("%d%s", &n, a);
  cout << func(n, a) << endl;
}
