#include <iostream>
#include <cmath>
#include <string>
#include <map>
#include <queue>
using namespace std;
#define N 9

struct data{
  int f[N];
  int move;
  string path;

  bool operator < (const data &p) const {
    for(int i = 0;i < N;i++){
      if(f[i] == p.f[i]) continue;
      return f[i] > p.f[i];
    }
    return false;
  }
};

static const int dx[4] = {-1, 0, 1, 0};
static const int dy[4] = {0, -1, 0, 1};
static const char where[4] = {'u', 'l', 'd', 'r'};

bool isTarget(data);
string bfs(data);

int main(){
  data a;
  int i;
  string c;

  for(i = 0;i < N;i++){
    cin >> a.f[i];
    if(a.f[i] == 0){
      a.f[i] = N;
      a.move = i;
    }
  }
  c = bfs(a);
  cout << c.size() << endl;

  return 0;
}

bool isTarget(data p){
  int i;
  
  for(i = 0;i < N;i++){
    if(p.f[i] != (i + 1)) return 0;
  }
    return 1;
}

string bfs(data p){
  queue<data> Q;
  map<data,bool> V;
  data x,y;
  int px,py,tx,ty,i;
  p.path = "";
  Q.push(p);
  V[p] = 1;

  while(!Q.empty()){
    x = Q.front(); Q.pop();
    if(isTarget(x)) return x.path;
    px = x.move / (N/3);
    py = x.move % (N/3);
    for(i = 0;i < 4;i++){
      tx = px + dx[i];
      ty = py + dy[i];
      if(tx < 0 || ty < 0 || tx >= (N/3) || ty >= (N/3)) continue;
      y = x;
      swap(y.f[x.move],y.f[tx * (N/3) + ty]);
      y.move = tx * (N/3) + ty;
      if(!V[y]){
	V[y] = 1;
	y.path += where[i];
	Q.push(y);
      }
    }
  }
  return "unsolvable";
}


### Prompt

Please formulate 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 <iostream>
#include <cmath>
#include <string>
#include <map>
#include <queue>
using namespace std;
#define N 9

struct data{
  int f[N];
  int move;
  string path;

  bool operator < (const data &p) const {
    for(int i = 0;i < N;i++){
      if(f[i] == p.f[i]) continue;
      return f[i] > p.f[i];
    }
    return false;
  }
};

static const int dx[4] = {-1, 0, 1, 0};
static const int dy[4] = {0, -1, 0, 1};
static const char where[4] = {'u', 'l', 'd', 'r'};

bool isTarget(data);
string bfs(data);

int main(){
  data a;
  int i;
  string c;

  for(i = 0;i < N;i++){
    cin >> a.f[i];
    if(a.f[i] == 0){
      a.f[i] = N;
      a.move = i;
    }
  }
  c = bfs(a);
  cout << c.size() << endl;

  return 0;
}

bool isTarget(data p){
  int i;
  
  for(i = 0;i < N;i++){
    if(p.f[i] != (i + 1)) return 0;
  }
    return 1;
}

string bfs(data p){
  queue<data> Q;
  map<data,bool> V;
  data x,y;
  int px,py,tx,ty,i;
  p.path = "";
  Q.push(p);
  V[p] = 1;

  while(!Q.empty()){
    x = Q.front(); Q.pop();
    if(isTarget(x)) return x.path;
    px = x.move / (N/3);
    py = x.move % (N/3);
    for(i = 0;i < 4;i++){
      tx = px + dx[i];
      ty = py + dy[i];
      if(tx < 0 || ty < 0 || tx >= (N/3) || ty >= (N/3)) continue;
      y = x;
      swap(y.f[x.move],y.f[tx * (N/3) + ty]);
      y.move = tx * (N/3) + ty;
      if(!V[y]){
	V[y] = 1;
	y.path += where[i];
	Q.push(y);
      }
    }
  }
  return "unsolvable";
}


```