N = int(input()) S = input() pair_sum = op_sum = cl_sum = 0 pair = op = cl = 0 for c in S: if c == '(': op += 1 else: if op > 0: op -= 1 pair += 1 else: cl += 1 op_sum += op cl_sum += cl pair_sum += pair for c in S: if c == '(': break cl_sum -= 1 print(pair_sum*2 + min(op_sum, cl_sum)*2)