#include<bits/stdc++.h>
using namespace std;
const int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
const char rd[4]={'u','d','l','r'};
struct gg{
	int mp[9];
	int space;
	string path;
	bool operator<(const gg &tmp)const
	{
		for(int i=0;i<=8;i++)
		   {
		   	if(mp[i]!=tmp.mp[i])return mp[i]>tmp.mp[i];
		   }
		return false;   
	}
};
bool ifok(gg a)
{
	for(int i=0;i<8;i++)
	  if(a.mp[i]!=i+1)return false;
	return true;  
}
gg bfs(gg bg)
{
	queue<gg> q;gg now,tmp;int x,y;
	map<gg,bool>vis;
	vis[bg]=1;
	q.push(bg);
	while(!q.empty())
	{   
		now=q.front();q.pop();
		if(ifok(now))return now;
		x=now.space%3;y=now.space/3;
		for(int i=0;i<=3;i++)
		   {
		     tmp=now;
		     if(x+dx[i]<0||x+dx[i]>2||y+dy[i]<0||y+dy[i]>2)continue;
		     tmp.space=x+dx[i]+(y+dy[i])*3;
		     swap(tmp.mp[x+y*3],tmp.mp[x+dx[i]+(y+dy[i])*3]);
		     tmp.path+=rd[i];
		     if(vis[tmp])continue;
		     vis[tmp]=1;q.push(tmp);
		   }
	}	
}
int main()
{
	gg bg,ans;
	for(int i=0;i<=8;i++)
	   {
	   	scanf("%d",&bg.mp[i]);
	   	if(bg.mp[i]==0)bg.space=i;
	   }   
	bg.path="";
	ans=bfs(bg);
	cout<<ans.path.length()<<endl;
}
### Prompt

Please provide a cpp coded solution to the problem described below:

The goal of the 8 puzzle problem is to complete pieces on $3 \times 3$ cells where one of the cells is empty space.

In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 8 as shown below.


1 3 0
4 2 5
7 8 6


You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).


1 2 3
4 5 6
7 8 0


Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.

Constraints

* There is a solution.

Input

The $3 \times 3$ integers denoting the pieces or space are given.

Output

Print the fewest steps in a line.

Example

Input

1 3 0
4 2 5
7 8 6


Output

4

### Response

```cpp
#include<bits/stdc++.h>
using namespace std;
const int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
const char rd[4]={'u','d','l','r'};
struct gg{
	int mp[9];
	int space;
	string path;
	bool operator<(const gg &tmp)const
	{
		for(int i=0;i<=8;i++)
		   {
		   	if(mp[i]!=tmp.mp[i])return mp[i]>tmp.mp[i];
		   }
		return false;   
	}
};
bool ifok(gg a)
{
	for(int i=0;i<8;i++)
	  if(a.mp[i]!=i+1)return false;
	return true;  
}
gg bfs(gg bg)
{
	queue<gg> q;gg now,tmp;int x,y;
	map<gg,bool>vis;
	vis[bg]=1;
	q.push(bg);
	while(!q.empty())
	{   
		now=q.front();q.pop();
		if(ifok(now))return now;
		x=now.space%3;y=now.space/3;
		for(int i=0;i<=3;i++)
		   {
		     tmp=now;
		     if(x+dx[i]<0||x+dx[i]>2||y+dy[i]<0||y+dy[i]>2)continue;
		     tmp.space=x+dx[i]+(y+dy[i])*3;
		     swap(tmp.mp[x+y*3],tmp.mp[x+dx[i]+(y+dy[i])*3]);
		     tmp.path+=rd[i];
		     if(vis[tmp])continue;
		     vis[tmp]=1;q.push(tmp);
		   }
	}	
}
int main()
{
	gg bg,ans;
	for(int i=0;i<=8;i++)
	   {
	   	scanf("%d",&bg.mp[i]);
	   	if(bg.mp[i]==0)bg.space=i;
	   }   
	bg.path="";
	ans=bfs(bg);
	cout<<ans.path.length()<<endl;
}
```