import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = (1 << 63)-1 inf = (1 << 31)-1 md = 10**9+7 # md = 998244353 n = II() s = SI() cs = [0] cnt = cur = 0 for c in s: if c == "(": cs.append(cs[-1]+1) cur += 1 else: cs.append(cs[-1]-1) cnt += cur mn = [inf] for s in cs[1:]: mn.append(min(mn[-1], s)) # pDB(cs) # pDB(mn) pos,neg=[],[] for s,m in zip(cs[1:],mn[1:]): if s>=0: pos.append((s,m)) else: neg.append((s,m)) pos.sort(key=lambda x:(-x[1],-x[0])) neg.sort(key=lambda x:(x[1]-x[0],-x[0])) # sm = [(s, m) for s, m in zip(cs[1:], mn[1:])] # sm.sort(key=lambda x: (-x[0], x[1])) now = 0 loss = 0 for s, m in pos+neg: loss += max(0, -now-m) now += s # pDB(s, m, now, loss) ans = max(0, min(cnt, n*(n+1)//2-cnt-loss)) print(ans*2)