#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main()
{
	string s;
	cin>>s;
	ll sl=stoll(s);
	string ans="0123456789";
	string m;
	ll mi=-1;
	do{
		ll tmp=0;
		for(int i=0;i<s.size();i++)
		{
			tmp*=10;
			tmp+=(ans[i]-'0');
		}
		ll diff=min(llabs(tmp-sl),(ll)pow(10,s.size())-llabs(tmp-sl));
		if(mi<diff)
		{
			//cerr<<diff<<endl;
			mi=diff;
			m=ans;
		}else if(mi==diff && m>ans){
			m=ans;
		}
	}while(next_permutation(ans.begin(),ans.end()));
	//解を出す
	ans="";
	for(int i=0;i<s.size();i++)
		ans+=m[i];
	cout<<ans<<endl;
	return 0;
}
