#include using namespace std; int main() { string s; cin >> s; sort(s.begin(), s.end(), greater<>()); string t = s; prev_permutation(t.begin(), t.end()); if (t[0] == '0' || s == t) { cout << "-1\n"; } else { cout << t << '\n'; } return 0; }