#T248. 回文数

回文数

Description

A number (with its first digit not being zero) is called a palindrome if it reads the same from left to right as it does from right to left.
For example: Given a decimal number 56, adding 56 and 65 (i.e., reading 56 from right to left) yields 121, which is a palindrome.
Another example, for the decimal number 87:
STEP1: 87 + 78 = 165
STEP2: 165 + 561 = 726
STEP3: 726 + 627 = 1353
STEP4: 1353 + 3531 = 4884
Here, a "step" refers to performing an addition in base N. In the above example, it took at least 4 steps to obtain the palindrome 4884.
Write a program that, given a base N (2 < N ≤ 10 or N = 16) and a number M in that base, finds the minimum number of steps required to obtain a palindrome.
If a palindrome cannot be obtained within 30 steps (inclusive), output "Impossible".

Input Format

Given a base N (2 < N ≤ 10 or N = 16) and a number M in that base.

Output Format

The minimum number of steps required. If a palindrome cannot be obtained within 30 steps (inclusive), output "Impossible".

9 87

6