#include using namespace std; typedef long long LL; int n, t, L, R; LL ans; char s[100005]; struct node { int L, R; bool operator<(const node &t) const { if (R == t.R) return L + t.R > t.L + R; return R < t.R; } }; vector p; int main() { #ifndef ONLINE_JUDGE freopen("data/data1.in", "r", stdin); // freopen("data/data1.out", "w", stdout); #endif scanf("%d%s", &n, s); for (int i = 0, t = 0; s[i]; i++) { if (s[i] == '(') L++; else L > 0 ? t++, L-- : R++; ans += t << 1; p.push_back({L, R}); } sort(p.begin(), p.end()); L = 0; for (auto &&t : p) { int p = min(L, t.R); L -= p; L += t.L; ans += p << 1; } cout << ans; return 0; }