#include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); vector> V = { {4,5,6}, {}, {0,2,3,4,6}, {0,2,3,5,6}, {1,2,3,5}, {0,1,3,5,6}, {1,3,4,5,6}, {0,2,5}, {}, {0,1,2,3,5} }; auto f = [&](long long x) -> bool { vector C(10); while(x) C.at(x%10)++,x /= 10; vector ap(7); for(int i=0; i<10; i++) if(C.at(i)) for(auto v : V.at(i)) ap.at(v) += C.at(i); for(int nine=0; nine<=C.at(9); nine++){ vector memo = ap; ap.at(6) += nine; int c = ap.at(6); int more = c-ap.at(3); bool ok = true; if(more >= 0 && more <= C.at(0)){ ap.at(3) += more; ap.at(0) += C.at(0)-more,ap.at(1) += C.at(0)-more,ap.at(2) += C.at(0)-more; more = c-ap.at(5); if(more >= 0 && more <= C.at(1)){ ap.at(2) += more,ap.at(5) += more; ap.at(1) += C.at(1)-more,ap.at(4) += C.at(1)-more; more = c-ap.at(0); if(more >= 0 && more <= C.at(6)){ ap.at(0) += more; more = c-ap.at(1); if(more >= 0 && more <= C.at(7)){ ap.at(1) += more; } else ok = false; } else ok = false; } else ok = false; } else ok = false; if(ok) for(int i=0; i<6; i++) if(ap.at(i) != ap.at(i+1)){ok = false; break;} if(ok) return true; ap = memo; } return false; }; long long N; cin >> N; while(f(N) == false) N++; cout << N << endl; }