#include using namespace std; typedef long long ll; int main() { string X; cin >> X; int l = X.length(); vector a(l); for (int i = 0; i < l; i++) { a[i] = X[i] - '0'; } sort(a.begin(), a.end(), greater()); if (prev_permutation(a.begin(), a.end())) { if (a[0] == 0) { cout << "-1"; } else { for (int i : a) { cout << i; } cout << "\n"; } } else { cout << "-1\n"; } return 0; }