This page is still under construction.

Parts of this page are still being built. What you see may change.

Balls in a Binary Tree

Interview

Time limit1sMemory limit128 MB

Summary
Follow the n-th ball down h levels of toggling switches, going left on odd visits and right on even visits, to find its leaf number.
Level

Medium5 of 10

Topics
Bit manipulation, Simulation, Math
Solved
No attempts yet

Problem

You are given a full binary tree of height hh (see the figure).

Every edge is either open or closed. Initially all left edges are open. Starting from the root, you drop nn balls one by one. Each ball always travels through the currently open edge; immediately after passing through it, that edge is closed and the sibling edge of the same vertex is opened. In other words, if the left edge was open, the left one is closed and the right one is opened; if the right edge was open, the right one is closed and the left one is opened.

Determine which leaf vertex the nn-th ball reaches. The bottom vertices are numbered from 00 to 2h−12^h - 1, left to right.

Input

The first line contains two integers nn and hh (1≤n≤1081 \le n \le 10^8, 0≤h≤300 \le h \le 30): the number of balls dropped and the height of the binary tree, respectively.

Output

Print a single integer: the number of the vertex the nn-th ball reaches.

Hint

For a tree of height 22, the balls land in vertices numbered 0,2,1,30, 2, 1, 3 in order, so the 4th ball reaches vertex 33. When h=0h = 0 the tree has a single vertex (numbered 00), so every ball lands in vertex 00.

Examples1

  1. Example 1

    Input
    4 2
    
    Expected output
    3