#include<bits/stdc++.h>
using namespace std;
int Y[4]={-1,0,1,0};
int X[4]={0,1,0,-1};
bool in(int a,int b){
  if(a<0||b<0||3<=a||3<=b)return false;
  return true;
}
int main(){
  int a;
  string start="",goal="123456780";
  for(int i=0;i<9;i++){
    cin>>a;
    start+=(a+'0');
  }
  queue<string> q;
  map<string,int> m;
  m[start]=0;
  q.push(start);
  while(!q.empty()){
    string u=q.front();
    q.pop();
    if(u==goal){
      cout<<m[u]<<endl;
      break;
    }
    int s=-1;
    for(int i=0;i<u.length();i++)if(u[i]=='0')s=i;
    int y=s/3,x=s%3;
    for(int i=0;i<4;i++){
      int ny=y+Y[i],nx=x+X[i];
      if(!in(ny,nx))continue;
      int g=ny*3+nx;
      string t=u;
      swap(t[s],t[g]);
      if(m.find(t)==m.end()){
        m[t]=m[u]+1;
        q.push(t);
      }
    }
  }
  return 0;
}



### 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;
int Y[4]={-1,0,1,0};
int X[4]={0,1,0,-1};
bool in(int a,int b){
  if(a<0||b<0||3<=a||3<=b)return false;
  return true;
}
int main(){
  int a;
  string start="",goal="123456780";
  for(int i=0;i<9;i++){
    cin>>a;
    start+=(a+'0');
  }
  queue<string> q;
  map<string,int> m;
  m[start]=0;
  q.push(start);
  while(!q.empty()){
    string u=q.front();
    q.pop();
    if(u==goal){
      cout<<m[u]<<endl;
      break;
    }
    int s=-1;
    for(int i=0;i<u.length();i++)if(u[i]=='0')s=i;
    int y=s/3,x=s%3;
    for(int i=0;i<4;i++){
      int ny=y+Y[i],nx=x+X[i];
      if(!in(ny,nx))continue;
      int g=ny*3+nx;
      string t=u;
      swap(t[s],t[g]);
      if(m.find(t)==m.end()){
        m[t]=m[u]+1;
        q.push(t);
      }
    }
  }
  return 0;
}



```