#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair P; int main(){ string s,t; cin >> s; t = s; sort(t.begin(), t.end()); next_permutation(t.begin(), t.end()); reverse(t.begin(), t.end()); next_permutation(t.begin(), t.end()); if (t == s) { cout << -1 << endl; } else { if (t[0] == '0') cout << -1 << endl; else cout << t << endl; } return 0; }