Super Divisible Numbers

A positive integer N is a super divisible number in the base B if and only if the number formed by the k most significant base-B digits of N is divisible by k, for all k between 1 and the number of base-B digits of N. (By definition, the most significant digit of N can not be 0.) For example, 22203014 is a base-4 super divisible number because 24 is divisible by 1, 224 is divisible by 2, 2224 is divisible by 3, 22204 is divisible by 4, 222034 is divisible by 5, 2220304 is divisible by 6, and 22203014 is divisible by 7. Find the largest super divisible number of a given base which uses only digits from a given list of digits. Input The input file contains several test cases. For each of them, the program input is a single line containing the base and the list of valid digits. The base will always be an integer between 4 and 10. The list of valid digits will have a length of at most 10, and may contain repeated digits (the repetitions should be ignored). Output For each test case, the program output is a single line containing the largest super divisible number of the given base which uses only the digits given on the input. It is known that the largest super divisible number will not have more than 50 digits. Examples Input 5 031322 7 01356 9 02468 10 01 10 123 10 9876543210 Sample Input 4 0123 10 010011 Sample Output 2220301 10 Output 2011021100 660060063531 886266840866066000 10 3212 3608528850368400786036725