Write a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2...XM (at least one), finds the smallest strictly positive multiple of N that has no other digits besides X1,X2...XM (if such a multiple exists). ????
nput
The input file has several data sets separated by an empty line, each data set having the following format:
On the first line - the number N
On the second line - the number M
On the following M lines - the digits X1,X2...XM.
Output
For each data set, the program should write to standard output on a single line the multiple, if such a multiple exists, and 0 otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sample Input
22
3
7
0
1
2
1
1
Sample Output
110
0
i dont understand why first answer is 110 and second one is 0
The digits in the set are not positions; {7, 0, 1} means a number with only using 7, 0, or 1 as a digit. Examples of this are 77, 0, 10, 700, 71, and of course 110. Example that do not work are 102 (has a 2, which is not in the set), 7713 (has a 3, which is not in the set), 56 (has a 5 and 6, neither of which are in the set).