#include int N; char S[100100]; int main() { scanf ("%d %s",&N,S+1); int ab = 0, amn = 0, b = 0, mn = 0; for (int i=1;i<=N;i++){ if (S[i] == '(') b++; else{ b--; if (mn > b) mn = b; } if (amn > ab + mn) amn = ab + mn; ab += b; } long long len = (long long)N * (N + 1) / 2 - ab + 2 * mn; printf ("%lld\n",len); return 0; }