Binary vs Decimal

Find the n-th positive integer whose decimal digits form a suffix of its binary representation.

Medium7Number theoryBit manipulationBrute forceNo attempts yetTime limit1sMemory limit256 MB

Problem

A positive integer is bindecimal if its decimal representation is a suffix of its binary representation. Both representations are written without leading zeros.

For example, 1010=1010210_{10} = 1010_2, and the decimal representation 10 matches the last two digits of the binary representation 1010, so 10 is bindecimal. On the other hand 101010=111111001021010_{10} = 1111110010_2, and 1010 is not a suffix of 1111110010, so 1010 is not bindecimal. Neither is 42, because 4210=101010242_{10} = 101010_2 and 42 is not a suffix of 101010.

Find the nn-th smallest bindecimal number.

Input

The first and only line contains one integer nn. (1n100001 \le n \le 10000)

Output

Print the nn-th smallest bindecimal number in decimal notation.

Hint

A binary representation uses only the digits 0 and 1, so the decimal representation of a bindecimal number also uses only 0 and 1. Here are the smallest numbers whose decimal representation contains only 0's and 1's.

DecimalBinaryComment
111st bindecimal number
1010102nd bindecimal number
1110113rd bindecimal number
10011001004th bindecimal number
10111001015th bindecimal number
11011011106th bindecimal number
11111011117th bindecimal number
100011111010008th bindecimal number
100111111010019th bindecimal number
10101111110010Not a bindecimal number
10111111110011Not a bindecimal number
11001000100110010th bindecimal number