#include using namespace std; string S; int N; vector ans; void f(int n, int mod, bool giri) { if (n == N) { if (mod != 0) { for (int d : ans) cout << d; cout << endl; exit(0); // 成功したら即終了 } else { return; } } if (giri) { for (int i = S[n] - '0'; i >= 0; --i) { if (i == 3) continue; if (i == S[n] - '0') { ans.push_back(i); f(n + 1, (mod + i) % 3, true); ans.pop_back(); } else { ans.push_back(i); f(n + 1, (mod + i) % 3, false); ans.pop_back(); } } } else { for (int i = 9; i >= 0; --i) { if (i == 3) continue; ans.push_back(i); f(n + 1, (mod + i) % 3, false); ans.pop_back(); } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> S; N = S.size(); f(0, 0, true); return 0; }