import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.stream.Collectors;
import java.stream.Stream;
public class Main{
public static final int NUM_0 = 0;
public static final int NUM_1 = 1;
public static final int NUM_2 = 2;
public static final int NUM_3 = 3;
public static final int NUM_4 = 4;
public static final int NUM_5 = 5;
public static final int NUM_6 = 6;
public static final int NUM_7 = 7;
public static final int NUM_8 = 8;
public static final int NUM_9 = 9;
public static final int NUM_1_NEGATIVE = -1;
public static final int NUM_2_NEGATIVE0 = -2;
public static final int NUM_3_NEGATIVE0 = -3;
public static final NUM_2_OR_5_USE_LIST = new int[]{0,2,4,6,8};
public static void main(String args[]){
String numStr=args[0];
List<Integer> singleNumStrList=Stream.of(numStr.split("")).map(x->Integer.valueOf(x)).collect(Collectors.toList());
while(true){
boolean flag=guessPrimeNumber(singleNumStrList);
if(flag){
System.out.println(singleNumStrList.stream().map(x->String.valueOf(x)).collect(Collectors.joining()));
return;
}
nextSingleNumStrList(singleNumStrList);
if(singleNumStrList.size()==NUM_1&&singleNumStrList.get(NUM_0)==NUM_0){
return;
}
}
}
private static void nextSingleNumStrList(List
for(int i=singleNumStrList.size()-NUM_1;i>=NUM_0;i--){
int num=singleNumStrList.get(i);
num=num-NUM_1;
if(num>=NUM_0){
singleNumStrList.set(i,num);
break;
}
singleNumStrList.set(i,NUM_9);
}
Iterator<Integer> ite=singleNumStrList.iterator();
while(ite.hasNext){
if(ite.next()==NUM_0){
ite.remove();
continue;
}
break;
}
}
private static void guessPrimeNumber(List
if(guessSingleNum(singleNumStrList)){
return true;
}
if(guessDivide2Or5(singleNumStrList)){
return false;
}
if(guessDivide3(singleNumStrList)){
return false;
}
if(guessDivide7(singleNumStrList)){
return false;
}
return true;
}
private static void guessSingleNum(List
if(singleNumStrList.size() == NUM_1){
int num=singleNumStrList.get(NUM_0);
return numNUM_1 || numNUM_2 || numNUM_3 || numNUM_5 || num==NUM_7;
}
return false;
}
private static void guessDivide2Or5(List
int lastNum=singleNumStrList.get(singleNumStrList.size()-NUM_1);
for(int num: NUM_2_OR_5_USE_LIST){
if(lastNum==num){
return true;
}
}
return false;
}
private static void guessDivide3(List
long sum=singleNumStrList.parallelStream().mapToInt(x->x).sum();
return sum%NUM_3==NUM_0;
}
private static void guessDivide7(List
List
for(int i=NUM_0,ringInex=NUM_1;i<singleNumStrList.size();i++,ringIndex++){
int calcNum=NUM_0;
switch(ringIndex){
case NUM_1:
calcNum=NUM_1;
break;
case NUM_2:
calcNum=NUM_3;
break;
case NUM_3:
calcNum=NUM_2;
break;
case NUM_1:
calcNum=NUM_1_NEGATIVE;
break;
case NUM_2:
calcNum=NUM_3_NEGATIVE;
break;
case NUM_3:
calcNum=NUM_2_NEGATIVE;
ringIndex=NUM_1;
break;
defualt:break;
}
tempList.add(singleNumStrList.get(i)*calcNum);
}
long sum=temList.parallelStream().mapToInt(x->x).sum();
return sum%NUM_7==NUM_0;
}
}