#include using namespace std; using ll = long long; int main() { cin.tie(0); ios::sync_with_stdio(false); string x; cin >> x; sort(x.begin(), x.end(), greater<>()); string x2 = x; prev_permutation(x2.begin(), x2.end()); if (x2[0] == '0' || x == x2) { cout << -1 << endl; } else { cout << x2 << endl; } return 0; }