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 pena = int(S[0] == ')') print(pair_sum*2 + max(0, min(op_sum, cl_sum) - pena)*2)