#P445. 【例78.3】回文数(Noip1999)
【例78.3】回文数(Noip1999)
Description
A number (with non-zero first digit) that reads the same from left to right and right to left is called a palindrome. For example, given a decimal number , adding and (reading from right to left) gives , which is a palindrome. Another example, for decimal number :
STEP1:
STEP2:
STEP3:
STEP4:
Here, a step means performing one addition in base . In the above example, it took at least steps to get the palindrome .
Write a program that, given a number in base ( or ), finds the minimum number of steps needed to obtain a palindrome. If it's impossible to get a palindrome within steps (inclusive), output "Impossible".
Input Format
The first line contains a number ( or ) representing the base;
The second line contains a number in base .
Output Format
The minimum number of steps needed. If it's impossible to get a palindrome within steps (inclusive), output "Impossible".
Sample
9
876