#define _USE_MATH_DEFINES #include using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int cnt[10] = {}; for (char c : s) cnt[c - '0']++; string t; for (int i = 9; i >= 0; i--) for (int j = 0; j < cnt[i]; j++) t += i + '0'; if (prev_permutation(t.begin(), t.end())) { if (t[0] == '0') { cout << -1 << '\n'; } else { cout << t << '\n'; } } else { cout << -1 << '\n'; } return 0; }