#include using namespace std; int main() { string S, T; cin >> S; sort(S.rbegin(), S.rend()); T = S; prev_permutation(S.begin(), S.end()); cout << ((S != T && S.at(0) != '0') ? S : "-1") << "\n"; }