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

#define n 3
#define n1 9


struct puzz{
  int m[n1];
  int space;
  string path;
  int operator < (const puzz &p)const{
    for(int i=0;i<n1;i++){
      if(m[i]==p.m[i])continue;
      return m[i] > p.m[i];
    }
    return false;
  }
};


static const int x[4]={-1,0,1,0};
static const int y[4]={0,-1,0,1};
static const char c[4]={'u','l','d','r'};

int target(puzz p){
  for(int i=0;i<n1;i++){
    if(p.m[i]!=(i+1))return false;
  }
  return true;
}

string am(puzz p){
  queue<puzz> Q;
  map<puzz, int> V;
  puzz a,b;
  int tmp;
  p.path="";
  Q.push(p);
  V[p]=true;

  while(!Q.empty()){
    a=Q.front();
    Q.pop();
    if(target(a))return a.path;
    int xx=a.space/n;
    int yy=a.space%n;
    for(int k=0;k<4;k++){
      int xc=xx+x[k];
      int yc=yy+y[k];
      if(xc<0||yc<0||xc>=n||yc>=n)continue;
      b=a;
      tmp=b.m[a.space];
      b.m[a.space]=b.m[xc*n+yc];
      b.m[xc*n+yc]=tmp;
      b.space=xc*n+yc;
      if(!V[b]){
	V[b]=true;
	b.path+=c[k];
	Q.push(b);
	
      }
    }
  }
  return "unsolvable";
}

int main(){
  puzz input;
  for(int i=0;i<n1;i++){
    cin>>input.m[i];
    if(input.m[i]==0){
      input.m[i]=n1;
      input.space=i;
    }
  }
  string ans =am(input);
  cout<<ans.size()<<endl;
  return 0;
}


### Prompt

Create a solution in CPP for 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;

#define n 3
#define n1 9


struct puzz{
  int m[n1];
  int space;
  string path;
  int operator < (const puzz &p)const{
    for(int i=0;i<n1;i++){
      if(m[i]==p.m[i])continue;
      return m[i] > p.m[i];
    }
    return false;
  }
};


static const int x[4]={-1,0,1,0};
static const int y[4]={0,-1,0,1};
static const char c[4]={'u','l','d','r'};

int target(puzz p){
  for(int i=0;i<n1;i++){
    if(p.m[i]!=(i+1))return false;
  }
  return true;
}

string am(puzz p){
  queue<puzz> Q;
  map<puzz, int> V;
  puzz a,b;
  int tmp;
  p.path="";
  Q.push(p);
  V[p]=true;

  while(!Q.empty()){
    a=Q.front();
    Q.pop();
    if(target(a))return a.path;
    int xx=a.space/n;
    int yy=a.space%n;
    for(int k=0;k<4;k++){
      int xc=xx+x[k];
      int yc=yy+y[k];
      if(xc<0||yc<0||xc>=n||yc>=n)continue;
      b=a;
      tmp=b.m[a.space];
      b.m[a.space]=b.m[xc*n+yc];
      b.m[xc*n+yc]=tmp;
      b.space=xc*n+yc;
      if(!V[b]){
	V[b]=true;
	b.path+=c[k];
	Q.push(b);
	
      }
    }
  }
  return "unsolvable";
}

int main(){
  puzz input;
  for(int i=0;i<n1;i++){
    cin>>input.m[i];
    if(input.m[i]==0){
      input.m[i]=n1;
      input.space=i;
    }
  }
  string ans =am(input);
  cout<<ans.size()<<endl;
  return 0;
}


```