Anagram and Multiplication

There are some amazing M-digit, N-based numbers which when multiplied by 2,3,...M, results in new numbers which are just anagrams of the original number. For example 142857 is such a 6-digit decimal (10-based) number as shown below: 2 x 142,857 = 285,714 3 x 142,857 = 428,571 4 x 142,857 = 571,428 5 x 142,857 = 714,285 6 x 142,857 = 857,142 Given the value of M and N your job is to find such a number. Without any proof I can assure you that for a single pair of value of M and N there is at most one such number. Input The input file contains maximum 30 lines of input. Each line contains two integers M (3 ≤ M ≤ 6) and N (4 ≤ N ≤ 400). The meaning of M and N is given in the problem statement. Input is terminated by a line where M = N = 0. This case should not be processed. Output For each line of input you should produce one line of output. This line should contain M integers separated by a single space. All these integers are within 0 and N − 1 (inclusive). These integers are the values of digits of the required N-based amazing number. The digits should be printed in descending order of significance (The left most integer is the value of the most significant digit and the rightmost integer is the value of the least significant digit of the desired number). If there is no such N based M-digit number print the string ‘Not found.’ (without the quotes) instead. Look at the output for sample input for details. Sample Input 6 10 6 100 00 Sample Output 142857 Not found.