#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) for(auto x : v){cout << x << " ";} cout << endl
#define printVS(vs) for(auto x : vs){cout << x << endl;}
#define printVV(vv) for(auto v : vv){for(auto&& x : v){cout << x << " ";}cout << endl;}
#define printP(p) cout << p.first << " " << p.second << endl
#define printVP(vp) for(auto p : vp) printP(p);

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<Pii> vp;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

int n;
int x;
bool used[10];
int ma_diff = -1;
int ans;

int diff(int a, int b) {
    return min(abs(a - b), (int)pow(10ll, n) - abs(a - b));
}

void dfs(int i, int y) {
    if (i == n) {
        int t = diff(x, y);
        if (ma_diff < t || (ma_diff == t && y < ans)) {
            ma_diff = t;
            ans = y;
        }
        return ;
    }

    rep(d, 10) {
        if (used[d]) continue;
        used[d] = true;
        dfs(i + 1, 10 * y + d);
        used[d] = false;
    }
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    string s;
    cin >> s;
    n = s.size();
    x = stoll(s);

    dfs(0, 0);

    cout << setw(n) << setfill('0') << ans << endl;
}