#include<bits/stdc++.h>
using namespace std;
long long n,Ans[100],a[100],vis[100],have=0,OUT[100];
long long c1[100],c2[100],AA=0;
long long ss;
char s[100];
long long BEST()
{
	long long tmp1=0,tmp2=0;
	for (long long i=1;i<=n;i++) 
	{
		tmp1=tmp1*10+Ans[i];
		tmp2=tmp2*10+OUT[i];
	}
	long long ck1=min(abs(tmp1-AA),ss*2-abs(tmp1-AA));
	long long ck2=min(abs(tmp2-AA),ss*2-abs(tmp2-AA));
	if (ck1>ck2) return 1;
	if (ck1<ck2) return 0;
	if (tmp1<tmp2) return 1;
	else return 0;
}
void DO()
{
	for (long long i=1;i<=n;i++) OUT[i]=Ans[i];
	return;
}
void DFS(long long dep,long long tag)
{
	if (dep>n)
	{
		if (!have) DO(),have=1;
		else if (BEST()) DO();
		return;
	}
	long long tmp=a[dep];
	long long tmp1,tmp2;
	if (tag==0)
	{
		if (!vis[tmp])
		{
			Ans[dep]=tmp;
			vis[tmp]=1;
			DFS(dep+1,0);
			vis[tmp]=0;
			Ans[dep]=0;
		}
		tmp1=tmp-1;
		while (vis[tmp1]&&tmp1-1>=0) tmp1--;	
		if (tmp1>=0&&!vis[tmp1])
		{
			Ans[dep]=tmp1;
			vis[tmp1]=1;
			DFS(dep+1,1);
			vis[tmp1]=0;
			Ans[dep]=0;
		}
		tmp2=tmp+1;
		while (vis[tmp2]&&tmp2+1<=9) tmp2++;
		if (tmp2<=9&&!vis[tmp2])
		{
			Ans[dep]=tmp2;
			vis[tmp2]=1;
			DFS(dep+1,-1);
			vis[tmp2]=0;
			Ans[dep]=0;
		}
	}
	if (tag==1)
	{
		tmp=9;
		while (vis[tmp]) tmp--;
		Ans[dep]=tmp;
		vis[tmp]=1;
		DFS(dep+1,1);
		vis[tmp]=0;
		Ans[dep]=0;
	}
	if (tag==-1)
	{
		tmp=0;
		while (vis[tmp]) tmp++;
		Ans[dep]=tmp;
		vis[tmp]=1;
		DFS(dep+1,-1);
		vis[tmp]=0;
		Ans[dep]=0;
	}
	return;
}
int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	for (long long i=1;i<=n;i++) a[i]=s[i]-'0';
	ss=5;
	for (long long i=1;i<n;i++) ss=ss*10;
	for (long long i=1;i<=n;i++) AA=AA*10+a[i];
	Ans[1]=(a[1]+5)%10;
	vis[Ans[1]]=1;
	DFS(2,0);
	for (long long i=1;i<=n;i++) printf("%lld",OUT[i]);
	printf("\n");
	return 0;
}

### Prompt

Please formulate a CPP solution to the following problem:

Example

Input

201


Output

701

### Response

```cpp
#include<bits/stdc++.h>
using namespace std;
long long n,Ans[100],a[100],vis[100],have=0,OUT[100];
long long c1[100],c2[100],AA=0;
long long ss;
char s[100];
long long BEST()
{
	long long tmp1=0,tmp2=0;
	for (long long i=1;i<=n;i++) 
	{
		tmp1=tmp1*10+Ans[i];
		tmp2=tmp2*10+OUT[i];
	}
	long long ck1=min(abs(tmp1-AA),ss*2-abs(tmp1-AA));
	long long ck2=min(abs(tmp2-AA),ss*2-abs(tmp2-AA));
	if (ck1>ck2) return 1;
	if (ck1<ck2) return 0;
	if (tmp1<tmp2) return 1;
	else return 0;
}
void DO()
{
	for (long long i=1;i<=n;i++) OUT[i]=Ans[i];
	return;
}
void DFS(long long dep,long long tag)
{
	if (dep>n)
	{
		if (!have) DO(),have=1;
		else if (BEST()) DO();
		return;
	}
	long long tmp=a[dep];
	long long tmp1,tmp2;
	if (tag==0)
	{
		if (!vis[tmp])
		{
			Ans[dep]=tmp;
			vis[tmp]=1;
			DFS(dep+1,0);
			vis[tmp]=0;
			Ans[dep]=0;
		}
		tmp1=tmp-1;
		while (vis[tmp1]&&tmp1-1>=0) tmp1--;	
		if (tmp1>=0&&!vis[tmp1])
		{
			Ans[dep]=tmp1;
			vis[tmp1]=1;
			DFS(dep+1,1);
			vis[tmp1]=0;
			Ans[dep]=0;
		}
		tmp2=tmp+1;
		while (vis[tmp2]&&tmp2+1<=9) tmp2++;
		if (tmp2<=9&&!vis[tmp2])
		{
			Ans[dep]=tmp2;
			vis[tmp2]=1;
			DFS(dep+1,-1);
			vis[tmp2]=0;
			Ans[dep]=0;
		}
	}
	if (tag==1)
	{
		tmp=9;
		while (vis[tmp]) tmp--;
		Ans[dep]=tmp;
		vis[tmp]=1;
		DFS(dep+1,1);
		vis[tmp]=0;
		Ans[dep]=0;
	}
	if (tag==-1)
	{
		tmp=0;
		while (vis[tmp]) tmp++;
		Ans[dep]=tmp;
		vis[tmp]=1;
		DFS(dep+1,-1);
		vis[tmp]=0;
		Ans[dep]=0;
	}
	return;
}
int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	for (long long i=1;i<=n;i++) a[i]=s[i]-'0';
	ss=5;
	for (long long i=1;i<n;i++) ss=ss*10;
	for (long long i=1;i<=n;i++) AA=AA*10+a[i];
	Ans[1]=(a[1]+5)%10;
	vis[Ans[1]]=1;
	DFS(2,0);
	for (long long i=1;i<=n;i++) printf("%lld",OUT[i]);
	printf("\n");
	return 0;
}

```