n = int(input()) s = input().strip() x = 0 while x < len(s) and s[x] == ')': x += 1 L_total = 0 R_total = 0 for j in range(n): if s[j] == '(': L_total += (n - j) else: R_total += (n - j) effective_r = max(0, R_total - x) ans = 2 * min(L_total, effective_r) print(ans)