Scanf.scanf "%d %d" (fun n x ->
    let total = Array.make (n + 1) 0 in
    let patty = Array.make (n + 1) 0 in
    total.(0) <- 1;
    patty.(0) <- 1;
    for i = 1 to n do
        total.(i) <- 2 * total.(i - 1) + 3;
        patty.(i) <- 2 * patty.(i - 1) + 1;
    done;

    let rec eat level rest =
        if level = 0 then min rest 1 else
        if rest <= 1 then 0 else
        if rest <= 1 + total.(level - 1) then eat (level - 1) (rest - 1) else 
        if rest <= 2 + total.(level - 1) then patty.(level - 1) + 1 else
            patty.(level - 1) + 1 + eat (level - 1) (rest - 2 - total.(level - 1))
    in
    eat n x |> Printf.printf "%d\n"
)