#include #define rep(i,n) for(int i=0;i<(n);++i) #define all(a) (a).begin(),(a).end() using namespace std; typedef long long ll; string f(string s) { string ret = ""; for (auto c: s) { ret += ('9' - c) + '0'; } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); string S; cin >> S; sort(S.rbegin(), S.rend()); string T = f(S); next_permutation(all(T)); T = f(T); if (T[0] == '0' || T == S) { cout << -1 << endl; } else { cout << T << endl; } return 0; }