N = gets.to_i S = gets.chomp open = close = set = 0 a = [] S.chars{|c| if c == ?( open += 1 else if open > 0 open -= 1 set += 1 else close += 1 end end a << [set, open, close] } a.sort_by!{|set, open, close| [-(open - close), -set] } open_cnt = close_cnt = cnt = 0 a.each_with_index{|(set, open, close), i| close_cnt += close if i != 0 open_cnt += open if i != a.size - 1 cnt += set } p (cnt + [close_cnt, open_cnt].min)*2