#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#define N 3
#define N2 9
#define ANS "123456780"

using namespace std;

// 上、左、右、下
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
struct Puzzle {
  string f;
  int space;
  int step;
};

void bfs (Puzzle p) {
  Puzzle cur;
  queue<Puzzle> Q;
  map<string, bool> M;

  Q.push(p);

  while (Q.size()) {
    cur = Q.front(); Q.pop();
    if (cur.f == ANS) {
      cout << cur.step << endl;
      break;
    }
    int sx = cur.space / N;
    int sy = cur.space % N;
    for (int r = 0; r < 4; r++) {
      int tx = sx + dx[r];
      int ty = sy + dy[r];
      if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue;
      Puzzle next;
      next.f = cur.f;
      swap(next.f[cur.space], next.f[tx * N + ty]);
      next.space = tx * N + ty;
      if (!M[next.f]) {
        M[next.f] = true;
        next.step = cur.step + 1;
        Q.push(next);
      }
    }
  }
}

int main () {
  Puzzle p;
  char c;
  
  p.step = 0;
  
  for (int i = 0; i < N2; i++) {
    cin >> c;
    p.f += c;
    if (c == '0') {
      p.space = i;
    }
  }

  bfs(p);

  return 0;
}

### 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 <cstdio>
#include <queue>
#include <map>
#define N 3
#define N2 9
#define ANS "123456780"

using namespace std;

// 上、左、右、下
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
struct Puzzle {
  string f;
  int space;
  int step;
};

void bfs (Puzzle p) {
  Puzzle cur;
  queue<Puzzle> Q;
  map<string, bool> M;

  Q.push(p);

  while (Q.size()) {
    cur = Q.front(); Q.pop();
    if (cur.f == ANS) {
      cout << cur.step << endl;
      break;
    }
    int sx = cur.space / N;
    int sy = cur.space % N;
    for (int r = 0; r < 4; r++) {
      int tx = sx + dx[r];
      int ty = sy + dy[r];
      if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue;
      Puzzle next;
      next.f = cur.f;
      swap(next.f[cur.space], next.f[tx * N + ty]);
      next.space = tx * N + ty;
      if (!M[next.f]) {
        M[next.f] = true;
        next.step = cur.step + 1;
        Q.push(next);
      }
    }
  }
}

int main () {
  Puzzle p;
  char c;
  
  p.step = 0;
  
  for (int i = 0; i < N2; i++) {
    cin >> c;
    p.f += c;
    if (c == '0') {
      p.space = i;
    }
  }

  bfs(p);

  return 0;
}

```