#include using namespace std; using ll = long long; using ul = unsigned long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; sort(s.rbegin(), s.rend()); if (!prev_permutation(s.begin(), s.end()) || s[0] == '0') cout << "-1\n"; else cout << s << "\n"; return 0; }