import java.util.Scanner; // 直接对应的进制进行相加,我一开始转成10进制后在加的,之前没有看清楚题目 public class Main { // 获取数的倒数(倒过来排序的数) public static String getPalindrome(String s) { String ss = ""; for (int i = s.length() - 1; i >= 0; i--) { ss += s.charAt(i); } return ss; } // 判断是否为回文数 public static boolean isPalindrome(String s) { int length = s.length(); for (int i = 0; i <= length / 2; i++) { if (s.charAt(i) != s.charAt(length - 1 - i)) { return false; } } return true; } // 进制相加 public static String add(String s1, String s2, int radix) { String ss = "0123456789ABCDEF"; int count = 0; StringBuffer sb = new StringBuffer(); for (int i = s1.length() - 1; i >= 0; i--) { int i1 = ss.indexOf(s1.charAt(i) + ""); int i2 = ss.indexOf(s2.charAt(i) + ""); int result = (i1 + i2 + count) % radix; count = (i1 + i2 + count) / radix; sb.insert(0, ss.charAt(result)); if (i == 0 && count == 1) { sb.insert(0, "1"); } } return sb.toString(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int radix = Integer.valueOf(sc.nextLine()); String value = sc.nextLine(); // 进制数 // System.out.println(value); // System.out.println(radix); int n = 0; // 加的次数 while (n <= 30) { // 判断是否为回文数 if (isPalindrome(value)) { break; } n++; value = add(value, getPalindrome(value), radix); // System.out.println(n + ":" + value); } if (n > 30) { System.out.print("Impossible!"); } else { System.out.print("STEP=" + n); } } }