#include <bits/stdc++.h>
using namespace std;

#define int long long

int n;
string s;
int t;
vector<int> all;
int pow_10;

void dfs(int m, int bit, int sum) {
  if ( m == n ) {
    if ( abs(sum-t) < pow_10/3 ) return;
    all.emplace_back(sum);
    return;
  }

  for ( int i = 0; i < 10; i++ ) {
    if ( bit & (1<<i) ) continue;
    int nxt = sum;
    nxt *= 10;
    nxt += i;
    dfs(m+1, bit|(1<<i), nxt);    
  }
}

struct St {
  int m, bit, sum;  
};

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> s;
  t = (int)stoll(s);  

  n = s.size();
  pow_10 = pow(10, n);  
  // dfs(0, 0, 0);
  
  stack<St> S;
  S.push({0, 0, 0});

  while ( !S.empty() ) {
    St p = S.top(); S.pop();
    int m = p.m, bit = p.bit, sum = p.sum;    
    if ( m == n ) {
      if ( abs(sum-t) < pow_10/3LL ) {
	continue;
      }
      all.emplace_back(sum);
      continue;      
    }

    for ( int i = 0; i < 10; i++ ) {
      if ( bit & (1LL<<i) ) continue;
      if ( m == 0 ) {
	if ( abs(i-(s[0]-'0')) > 5 ) {
	  continue;
	}
      }
      int nxt = sum;
      nxt *= 10LL;
      nxt += i;
      // dfs(m+1, bit|(1<<i), nxt);      
      S.push({m+1, bit|(1LL<<i), nxt});      
    }
  }

  sort(all.begin(), all.end());  

  int ans = t;
  int ma = 0;
  for ( int i = 0; i < (int)all.size(); i++ ) {
    if ( ma < min(abs(all[i]-t), pow_10-abs(all[i]-t)) ) {      
      ma = min(abs(all[i]-t), pow_10-abs(all[i]-t));      
      ans = all[i];      
    }
  }

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

