#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<vector<vector<char> >,int> P;
const long long int MOD = 1000000007;
const int INF = 1000000000;

string str;
ll n;
ll p = 1;
int s;
ll num[10];
ll ans = 0;
ll out;

int main(){
    cin >> str;
    s = str.size();
    rep(i,s){
        n = n*10+str[i]-'0';
        p *= 10;
    }
    rep(i,10) num[i] = i;
    do{
        ll x = 0;
        rep(i,s){
            x = x*10+num[i];
        }
        ll pre = min(abs(x-n),p-abs(x-n));
        if(pre > ans || (pre==ans && out > x)){
            ans = pre;
            out = x;
        }
        
    } while(next_permutation(num,num+10));
    ll pp = 1;
    ll zu = 0;
    while(pp <= out){
        pp *= 10;
        zu++;
    }
    rep(i,s-zu) cout << 0;
    cout << out << endl;
}