Anagrammatic Primes 

A number greater than one is prime if it has no divisors other than itself and 1 (note that 1 is not prime). For example, 23 is prime and 35 is not prime because 35 = 7 × 5. When the digits of a number are rearranged, its primeness may change - for example, 35 is not prime but 53 is. For this problem, you have to find numbers which are prime no matter how you rearrange their digits. For example, all of the numbers 113, 131 and 311 are prime, so we say that 113 is an anagrammatic prime (also 131 and 311 are anagrammatic primes).

Input 

Each line of input will contain a single positive number n, less than 10,000,000. You must find the first anagrammatic prime which is larger than n and less than the next power of 10 greater than n. The input will be terminated by a line consisting of a single `0'.

Output 

For each number in the input, one line of output must be produced. This output line should contain either the next anagrammatic prime, as described above, or `0' if there is no anagrammatic prime in the range defined.

Sample Input 

10
16
900
113
8000000
0

Sample Output 

11
17
919
131
0



Miguel Revilla 2004-07-08