# hdoj 4235 Vampire Numbers

！！！对于a * b已经大于最大符合条件的数的情况，即可break

//hdoj 4235 Vampire Numbers
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define max(a,b) ((a) > (b)) ? (a) : (b)
#define min(a,b) ((a) < (b)) ? (a) : (b)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x)

const int MAXN = 1110;
const int MAXM = 10011;
const int hash_size = 25000002;
const int INF = 10000000;

int x, cnt;
int endd[MAXM];

bool ok(int a, int b) {
int na = 0, nb = 0;
int v1[10], v2[10];
int n = a*b;
while (n) {
v1[na++] = n%10, n /= 10;
}
while (a) {
v2[nb++] = a%10, a /= 10;
}
while (b) {
v2[nb++] = b%10, b /= 10;
}
if (na != nb)
return false;
else {
sort(v1, v1+na);
sort(v2, v2+nb);
int i;
for (i = 0; i < na; ++i) {
if (v1[i] != v2[i])
return false;
}
return true;
}
}

void create() {
int i, j, tmp;
FORE(i, 2, 9) FORE(j, 2, 999999) {
if(i * j > 1000255)
break;
if(ok(i, j))
endd[cnt++] = i * j;
}
FORE(i, 10, 99) FORE(j, 10, 99999) {
if(i * j > 1000255)
break;
if(ok(i, j))
endd[cnt++] = i * j;
}
FORE(i, 100, 999) FORE(j, 100, 9999) {
if(i * j > 1000255)
break;
if(ok(i, j))
endd[cnt++] = i * j;
}
sort(endd, endd + cnt);
}

int bsh(int x) {
int l = 0, r = cnt - 1;
while(l <= r) {
int mid = (l + r) >> 1;
if(endd[mid] < x)
l = mid + 1;
else
r = mid - 1;
}
return endd[l];
}

int main() {
create();
while(scanf("%d", &x) != EOF, x) {
int tt = bsh(x);
printf("%d\n", tt);
}
return 0;
}