#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vb = vector<bool>;
using ii = pair<ll, ll>;
constexpr bool LOG = false;
void Log() {
  if (LOG) cerr << "\n";
}
template <class T, class... S>
void Log(T t, S... s) {
  if (LOG) cerr << t << "\t", Log(s...);
}
struct SuffixArray {
  string s;
  int n;
  vvi P;
  SuffixArray(string &_s) : s(_s), n(_s.length()) { construct(); }
  void construct() {
    P.push_back(vi(n, 0));
    compress();
    vi occ(n + 1, 0), s1(n, 0), s2(n, 0);
    for (int k = 1, cnt = 1; cnt / 2 < n; ++k, cnt *= 2) {
      P.push_back(vi(n, 0));
      fill(occ.begin(), occ.end(), 0);
      for (int i = 0; i < n; ++i)
        occ[i + cnt < n ? P[k - 1][i + cnt] + 1 : 0]++;
      partial_sum(occ.begin(), occ.end(), occ.begin());
      for (int i = n - 1; i >= 0; --i)
        s1[--occ[i + cnt < n ? P[k - 1][i + cnt] + 1 : 0]] = i;
      fill(occ.begin(), occ.end(), 0);
      for (int i = 0; i < n; ++i) occ[P[k - 1][s1[i]]]++;
      partial_sum(occ.begin(), occ.end(), occ.begin());
      for (int i = n - 1; i >= 0; --i) s2[--occ[P[k - 1][s1[i]]]] = s1[i];
      for (int i = 1; i < n; ++i) {
        P[k][s2[i]] = same(s2[i], s2[i - 1], k, cnt) ? P[k][s2[i - 1]] : i;
      }
    }
  }
  bool same(int i, int j, int k, int l) {
    return P[k - 1][i] == P[k - 1][j] && (i + l < n ? P[k - 1][i + l] : -1) ==
                                             (j + l < n ? P[k - 1][j + l] : -1);
  }
  void compress() {
    vi cnt(256, 0);
    for (int i = 0; i < n; ++i) cnt[s[i]]++;
    for (int i = 0, mp = 0; i < 256; ++i)
      if (cnt[i] > 0) cnt[i] = mp++;
    for (int i = 0; i < n; ++i) P[0][i] = cnt[s[i]];
  }
  vi &get_array() { return P.back(); }
  int lcp(int x, int y) {
    int ret = 0;
    if (x == y) return n - x;
    for (int k = P.size() - 1; k >= 0 && x < n && y < n; --k)
      if (P[k][x] == P[k][y]) {
        x += 1 << k;
        y += 1 << k;
        ret += 1 << k;
      }
    return ret;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vi p(n, -1);
  vi pc(n, -1);
  ll offset = n;
  vector<vi> childs(2 * n);
  stack<ll> parents;
  ll root = -1;
  for (auto i = decltype(n)(0); i < (n); ++i) {
    if (s[i] == '(') {
      if (parents.empty())
        p[i] = root;
      else
        p[i] = parents.top();
      parents.push(i);
      pc[i] = childs[p[i] + offset].size();
    } else {
      if (parents.empty())
        p[i] = --root;
      else {
        parents.pop();
        if (parents.empty())
          p[i] = root;
        else
          p[i] = parents.top();
        childs[p[i] + offset].push_back(i);
      }
    }
  }
  Log("i", "p", "pc", childs[0].size(), "children 0:");
  for (auto i = decltype(n)(0); i < (n); ++i)
    Log(i, p[i], pc[i], childs[i + 1].size());
  SuffixArray sa(s);
  vi &v = sa.get_array();
  vi w(n);
  for (auto i = decltype(n)(0); i < (n); ++i) w[v[i]] = i;
  ll ans = 0;
  for (auto i = decltype(n)(0); i < (n); ++i) {
    if (s[w[i]] == ')') continue;
    ll lcp = 0;
    if (i) lcp = sa.lcp(w[i - 1], w[i]);
    ll a = w[i];
    auto &cs = childs[p[a] + offset];
    ll start = 0;
    start = lower_bound(begin(cs), end(cs), a + lcp) - cs.begin();
    ans += cs.size() - start;
  }
  cout << ans << endl;
  return 0;
}
