N = int(input()) S = input() R = 0 # ((( LR = [] # ))(( L = 0 # )) ans = 0 add = 0 cntL = 0 cntR = 0 for s in S: if s == ")": if cntR: cntR -= 1 add += 2 else: cntL += 1 elif s == "(": cntR += 1 ans += add if cntL and cntR: LR.append((cntL, cntR)) elif cntL: L += cntL elif cntR: R += cntR LR.sort(key=lambda x: (x[0], -x[1])) for l, r in LR: d = min(R, l) ans += 2 * d R -= d R += r d = min(R, L) ans += 2 * d print(ans)