class Calc0022 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @n, @k = args.shift.map(&:to_i) @s = args.shift.first end def run cs = @s.split(//).map { |c| c == '(' ? 1 : -1 } if cs[@k - 1] == 1 find(cs[(@k - 1)..-1]) + @k else @k - find(cs[0..(@k - 1)].reverse) end end def find(cs) s = 0 cs.each_with_index do |c, i| s += c return i if s == 0 end end end puts Calc0022.new(STDIN.readlines).run if __FILE__ == $0