import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) S = read().rstrip().decode() L = '(' R = ')' dp = [(0, 0)] total_pair = 0 now_pair = 0 for x in S: a, b = dp[-1] if x == L: dp.append((a, b + 1)) else: if b: now_pair += 1 dp.append((a, b - 1)) else: dp.append((a + 1, 0)) total_pair += now_pair dp.sort(key=lambda x: min(x)) left = [] right = [] for a, b in dp: if a <= b: left.append((a, b)) else: right.append((a, b)) x = 0 y = 0 for a, b in left + right[::-1]: p = min(y, a) total_pair += p y -= p a -= p if not y: x, y = x + a, b else: x, y = x, y + b print(total_pair * 2)