#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,N;
string str,ans;
int used[10],dig[10];

ll s2i(string a){
  ll res=0;
  for(int i=0;i<a.size();i++) res=res*10+a[i]-'0';
  return res;
}

string i2s(int a[10]){
  string res;
  for(int i=0;i<n;i++) res+=a[i]+'0';
  return res;
}

bool check(){
  ll p=s2i(str);
  ll a=s2i(ans);
  ll b=s2i(i2s(dig));
  ll A=min(abs(p-a),N-abs(p-a));
  ll B=min(abs(p-b),N-abs(p-b));
  return (A<B)||(A==B&&a>b);
}

void dfs(int idx){
  if(idx==n&&check())ans=i2s(dig);
  if(idx==n)return;
  for(int i=0;i<10;i++){
    if(used[i])continue;
    used[i]=1;
    dig[idx]=i;
    dfs(idx+1);
    used[i]=0;
  }
}

int main(){
  cin>>str;
  n=str.size();
  memset(dig,-1,sizeof(dig));
  N=1;
  for(int i=0;i<n;i++) N*=10,ans+=i+'0';
  dfs(0);
  cout <<ans<<endl;
  return 0;
}