// Arup Guha
// 11/1/2011
// Solution to 2011 South East Regional Problem J: Vampire Numbers

import java.util.*;

public class j {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		// isVampNum[i] will store if i is a Vampire Number or not.
		/*** Note we make this array bigger than 1000000 because we must find the first Vampire Number
		 *   greater than 1000000
		 ***/
		boolean[] isVampNum = new boolean[1001000];
		for (int i=0; i<isVampNum.length; i++)
			isVampNum[i] = false;

		// Try all possible ways to multiply two numbers that are in range.
		for (int small = 1; small <= 2000; small++) {
			for (int large = small+1; small*large <= 1001000; large++) {
				if (check(small, large, small*large))
					isVampNum[small*large] = true;
			}
		}

		// Copy all Vampire Numbers into an ArrayList.
		ArrayList<Integer> allnums = new ArrayList<Integer>();
		for (int i=0; i<isVampNum.length; i++)
			if (isVampNum[i])
				allnums.add(i);

		// Get the first input.
		int number = stdin.nextInt();

		while (number != 0) {

			// Just do a linear search for the first Vampire Number that is big enough.
			for (int i=0; i<allnums.size(); i++) {
				if (allnums.get(i) >= number) {
					System.out.println(allnums.get(i));
					break;
				}
			}

			number = stdin.nextInt();
		}

	}

	// Returns if the digits in a and b are the same as the digits in v.
	public static boolean check(int a, int b, int v) {

		int[] freqleft = new int[10];
		int[] freqright =new int[10];

		// Initialize frequencies.
		for (int i=0; i<10; i++) {
			freqleft[i] = 0;
			freqright[i] = 0;
		}

		// Add in all the digits to the appropriate sides.
		updatefreq(a, freqleft);
		updatefreq(b, freqleft);
		updatefreq(v, freqright);

		// It matches if these arrays are the same.
		return equal(freqleft, freqright);
	}

	// Returns true iff the length of both arrays is the same and each corresponding element is equal.
	public static boolean equal(int[] f1, int[] f2) {

		if (f1.length != f2.length)
			return false;

		for (int i=0; i<f1.length; i++)
			if (f1[i] != f2[i])
				return false;

		return true;

	}

	// Updates the frequency of digits in freq by adding in all the digits in the integer a.
	public static void updatefreq(int a, int[] freq) {

		while (a > 0) {
			freq[a%10]++;
			a /= 10;
		}
	}

}