N, M = gets.split.map(&:to_i) def f(x) sum = 1 m = x remain = N while remain > 1 && m > 1 sum += m if Math.log2(m).ceil < remain m = (m + 1) / 2 else m = m / 2 end remain -= 1 end if m != 1 -1 else sum + remain - 1 end end ok = 1 ng = M + 1 while (ok - ng).abs >= 2 x = (ok + ng) / 2 val = f(x) if val != -1 ok = x else ng = x end end puts f(ok)