#include<bits/stdc++.h>
using namespace std;

struct P{
  int f[9];
  int t;
  int st;
  bool operator < (const P &p) const{
    for(int i=0;i<9;i++){
      if(f[i]==p.f[i])continue;
      return f[i]>p.f[i];
    }
    return false;
  }
};

int main(){
  P in;
  map<P,bool> V;
  queue<P> Q;
  for(int i=0;i<9;i++){
    cin>>in.f[i];
    if(in.f[i]==0)in.st=i;
  }
  in.t=0;
  Q.push(in);
  while(!Q.empty()){
    P top=Q.front();
    Q.pop();
    if(!V[top]){
      V[top]=true;
      int flag=0;
      for(int i=0;i<9;i++)if((i+1)%9!=top.f[i])flag=1;
      if(!flag){
	cout<<top.t<<endl;
	break;
      }
      int st=top.st;
      top.t++;
      P tmp;
      if(st-3>=0){
	tmp=top;
	swap(tmp.f[st],tmp.f[st-3]);
	tmp.st-=3;
	Q.push(tmp);
      }
      if(st+3<9){
	tmp=top;
	swap(tmp.f[st],tmp.f[st+3]);
	tmp.st+=3;
	Q.push(tmp);
      }
      if(st%3!=2){
	tmp=top;
	swap(tmp.f[st],tmp.f[st+1]);
	tmp.st++;
	Q.push(tmp);
      }
      if(st%3!=0){
	tmp=top;
	swap(tmp.f[st],tmp.f[st-1]);
	tmp.st--;
	Q.push(tmp);
      }
    }
  }
  return 0;
}
### Prompt

Your task is to create a cpp solution to the following problem:

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;

struct P{
  int f[9];
  int t;
  int st;
  bool operator < (const P &p) const{
    for(int i=0;i<9;i++){
      if(f[i]==p.f[i])continue;
      return f[i]>p.f[i];
    }
    return false;
  }
};

int main(){
  P in;
  map<P,bool> V;
  queue<P> Q;
  for(int i=0;i<9;i++){
    cin>>in.f[i];
    if(in.f[i]==0)in.st=i;
  }
  in.t=0;
  Q.push(in);
  while(!Q.empty()){
    P top=Q.front();
    Q.pop();
    if(!V[top]){
      V[top]=true;
      int flag=0;
      for(int i=0;i<9;i++)if((i+1)%9!=top.f[i])flag=1;
      if(!flag){
	cout<<top.t<<endl;
	break;
      }
      int st=top.st;
      top.t++;
      P tmp;
      if(st-3>=0){
	tmp=top;
	swap(tmp.f[st],tmp.f[st-3]);
	tmp.st-=3;
	Q.push(tmp);
      }
      if(st+3<9){
	tmp=top;
	swap(tmp.f[st],tmp.f[st+3]);
	tmp.st+=3;
	Q.push(tmp);
      }
      if(st%3!=2){
	tmp=top;
	swap(tmp.f[st],tmp.f[st+1]);
	tmp.st++;
	Q.push(tmp);
      }
      if(st%3!=0){
	tmp=top;
	swap(tmp.f[st],tmp.f[st-1]);
	tmp.st--;
	Q.push(tmp);
      }
    }
  }
  return 0;
}
```