#include #define rep(i,n) for(int i=0;i> x; sort(all(x)); reverse(all(x)); int n = x.size(); bool flg = false; for(int i = 1; i < n; ++i) { if(x[i - 1] != x[i]) flg = true; } if(not flg) { cout << -1 << endl; return 0; } string y = x; int cnt = 0; do { if(x != y and y.front() != '0') { cout << y << endl; return 0; } ++cnt; if(cnt == 2) break; }while(prev_permutation(all(y))); cout << -1 << endl; return 0; }