#include <bits/stdc++.h>
using namespace std;
template <class T>
T ceilPow2(T x) {
  T r = 1;
  while (r < x) {
    r <<= 1;
  }
  return r;
}
template <class Node>
struct TournamentTree {
  template <class B>
  void init(const B *b, int n, int start = 0) {
    this->n = n;
    this->start = start;
    nPow2 = ceilPow2(n);
    t.resize(nPow2 + n);
    for (int x = nPow2; x < nPow2 + n; ++x) {
      t[x] = Node(b[x - nPow2]);
    }
    for (int prevN = n, p2 = nPow2 >> 1; p2; p2 >>= 1, prevN = prevN + 1 >> 1) {
      int x;
      for (x = p2; x < p2 + (prevN >> 1); ++x) {
        t[x] = Node(t[2 * x], t[2 * x + 1]);
      }
      if (prevN & 1) {
        t[x] = t[2 * x];
      }
    }
  }
  template <class Ret, class... Args>
  struct Query {
    const int i, j;
    tuple<Args...> ta;
    TournamentTree &tt;
  };
  template <class Ret>
  Ret foldLeft(int i, int j, Ret r = Ret()) {
    foldLeftRec(i - start, j - start, r, 0, nPow2, 1);
    return r;
  }

 private:
  template <class Ret>
  void foldLeftRec(int i, int j, Ret &v, int a, int b, int x) {
    if (i <= a && b <= j) {
      v += t[x];
      return;
    }
    int mid = a + b >> 1;
    if (i < mid) {
      foldLeftRec(i, j, v, a, mid, 2 * x);
    }
    if (j > mid) {
      foldLeftRec(i, j, v, mid, b, 2 * x + 1);
    }
  }
  int n;
  int start;
  int nPow2;
  vector<Node> t;
};
void radixPass(const vector<int> &a, vector<int> &b, const int *r, int k) {
  vector<int> count(k + 2, 0);
  for (int x : a) {
    ++count[r[x] + 1];
  }
  int sum = 0;
  for (int &c : count) {
    int t = c;
    c = sum;
    sum += t;
  }
  for (int x : a) {
    b[count[r[x] + 1]++] = x;
  }
}
void suffixArray(vector<int> &s, vector<int> &SA, int K) {
  int n = s.size();
  s.resize(n + 3);
  s[n] = s[n + 1] = s[n + 2] = -1;
  int *ps = s.data();
  int n0 = (n + 2) / 3;
  int n1 = (n + 1) / 3;
  int n2 = (n + 0) / 3;
  int n02 = n0 + n2;
  vector<int> s12;
  s12.reserve(n02 + 3);
  vector<int> SA12(n02);
  for (int i = 0; i < n + (n0 - n1); ++i) {
    if (i % 3) {
      s12.push_back(i);
    }
  }
  radixPass(s12, SA12, ps + 2, K);
  radixPass(SA12, s12, ps + 1, K);
  radixPass(s12, SA12, ps + 0, K);
  int name = 0;
  int l = SA12[0];
  for (int e : SA12) {
    name += !equal(ps + l, ps + l + 3, ps + e);
    if (e % 3 == 1) {
      s12[e / 3] = name;
    } else {
      s12[e / 3 + n0] = name;
    }
    l = e;
  }
  if (name < n02 - 1) {
    suffixArray(s12, SA12, name);
    for (int i = 0; i < n02; ++i) {
      s12[SA12[i]] = i;
    }
  } else {
    for (int i = 0; i < n02; ++i) {
      SA12[s12[i]] = i;
    }
  }
  vector<int> s0;
  s0.reserve(n0);
  for (int &r : SA12) {
    if (r < n0) {
      s0.push_back(r * 3);
    }
    r = r < n0 ? r * 3 + 1 : (r - n0) * 3 + 2;
  }
  vector<int> SA0(n0);
  radixPass(s0, SA0, ps, K);
  s12.resize(n02 + 3);
  s12[n02] = s12[n02 + 1] = s12[n02 + 2] = -1;
  merge(SA0.begin(), SA0.end(), SA12.begin() + n0 - n1, SA12.end(), SA.begin(),
        [&](int i, int j) {
          return i % 3 == 1 ? make_tuple(s[i], s12[i / 3 + n0]) <
                                  make_tuple(s[j], s12[j / 3])
                            : make_tuple(s[i], s[i + 1], s12[i / 3 + 1]) <
                                  make_tuple(s[j], s[j + 1], s12[j / 3 + n0]);
        });
  s.resize(n);
}
vector<int> longestCommonPrefix(vector<int> &s, const vector<int> &sa, int n) {
  vector<int> lcp(n);
  s.push_back(-1);
  vector<int> rank(n);
  for (int i = 0; i < n; ++i) {
    rank[sa[i]] = i;
  }
  int l = 0;
  for (int i = 0; i < n; ++i) {
    l = max(l - 1, 0);
    int r = rank[i];
    if (r) {
      int j = sa[r - 1];
      while (s[i + l] == s[j + l]) {
        ++l;
      }
    } else {
      l = 0;
    }
    lcp[r] = l;
  }
  s.pop_back();
  return lcp;
}
struct Node {
  Node() = default;
  Node(char c) : mid(0) {
    if (c == ')') {
      pre.push_back(0);
    } else {
      suf.push_back(0);
    }
  }
  Node(const Node &l, const Node &r) : pre(l.pre), suf(r.suf) {
    if (l.suf.size() == r.pre.size()) {
      mid = l.mid + (l.suf.size() > 0) + r.mid;
    } else if (l.suf.size() < r.pre.size()) {
      pre.push_back(l.mid + (l.suf.size() > 0) + r.pre[l.suf.size()]);
      for (int i = l.suf.size() + 1; i < r.pre.size(); ++i) {
        pre.push_back(r.pre[i]);
      }
      mid = r.mid;
    } else {
      suf.push_back(r.mid + (r.pre.size() > 0) + l.suf[r.pre.size()]);
      for (int i = r.pre.size() + 1; i < l.suf.size(); ++i) {
        suf.push_back(l.suf[i]);
      }
      mid = l.mid;
    }
  }
  vector<int> pre;
  vector<int> suf;
  int mid;
};
struct Ret {
  Ret &operator+=(const Node &n) {
    if (open == -1) {
      return *this;
    }
    if (n.pre.size() < open) {
      open -= n.pre.size();
      open += n.suf.size();
    } else if (n.pre.size() == open) {
      matched += (open > 0) + n.mid;
      open = n.suf.size();
    } else {
      matched += (open > 0) + n.pre[open];
      open = -1;
    }
    return *this;
  }
  int matched = 0;
  int open = 0;
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  TournamentTree<Node> tt;
  tt.init(s.data(), n);
  vector<int> sc;
  sc.reserve(n + 3);
  for (char c : s) {
    sc.push_back(c == ')' ? 0 : 1);
  }
  vector<int> sa(n);
  suffixArray(sc, sa, 1);
  vector<int> lcp = longestCommonPrefix(sc, sa, n);
  long long sol = 0;
  for (int i = 0; i < n; ++i) {
    sol += tt.foldLeft<Ret>(sa[i], n).matched;
    if (lcp[i]) {
      sol -= tt.foldLeft<Ret>(sa[i], sa[i] + lcp[i]).matched;
    }
  }
  cout << sol << endl;
  return 0;
}
