m@main=do n<-readLn;print$head[z|z<-n:[n+x*y|x<-[1..],y<-[-1,1]],z#2>0];m n#p|p*p>n=n n#p|n`mod`p>0=n#(p+1) n#p=0