n = int(input()) S = input().strip() ans = 0 mi = 0 tot = 0 flg = False flg2 = False for s in S: if s == "(": tot += 1 flg = True elif s == ")": tot -= 1 flg2 = True if tot < mi: mi = tot if flg: ans += 1 flg = False flg2 = False else: if flg: tot -= 1 flg2 = True if tot < mi: mi = tot if flg: ans += 1 flg = False flg2 = False else: tot += 1 flg = True ans += flg2 print(ans)