#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//回文日期要以年为划分,枚举所有的回文日期再判断的方式更加简便
const int N = 9999;//要遍历所有比输入年份大的情况,那么就是从输入年份到9999年,不是到9000年!
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int nyear,nmonth,nday;
bool check1(int year,int month,int day){
//cout<<year<<" "<<month<<" "<<day<<endl;
if(month<1||month>12||day>31){
return false;
}else{
//cout<<year<<" "<<month<<" "<<day<<endl;
int flag = 0;
if(month==2){
if((year%4==0&&year%100)||(year%400==0)){
flag = 1;
}
}
if(day>months[month]+flag){
//cout<<year<<" "<<month<<" "<<day<<endl;
return false;
}
if((year==nyear&&month<nmonth)||(year==nyear&&month==nmonth&&day<=nday)){
return false;
}
}
return true;
}
bool check2(int year,int month,int day){
bool flag = check1(year,month,day);
if(flag){
//cout<<year<<" "<<month<<" "<<day<<endl;
return true;
}
}
int seek(int n){
nyear = n/10000;
nmonth = (n%10000)/100;
nday = n%100;
for(int i = nyear;i<=N;i++){
int year = i;
int month = (year%10)*10+(year/10)%10;
int day = ((year/100)%10)*10+year/1000;
bool ans1 = check1(year,month,day);
if(ans1){
int num1 = year*10000 + month*100+day;
cout<<num1<<endl;
break;
}
}
for(int i = nyear;i<=N;i++){
int year = i;
int month = (year%10)*10+(year/10)%10;
int day = ((year/100)%10)*10+year/1000;
if(year/100==year%100&&(year%10!=(year/10)%10)){//如果是ABAB一定是BABA的,并且要保证A≠B
bool ans2 = check2(year,month,day);
if(ans2){
int num2 = year*10000 + month*100+day;
cout<<num2<<endl;
break;
}
}
}
}
int main(){
int n ;
cin>>n;
seek(n);
return 0;
}