#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <unordered_map>

int main() {
  std::unordered_map<std::string, int> ss;
  std::queue<std::pair<std::string, int>> q;
  q.push(std::make_pair("123456780", 0));
  ss["123456780"] = 0;
  while (q.size()) {
    auto p = q.front();
    q.pop();
    auto s = p.first;
    auto d = p.second;
    auto z = std::find(s.begin(), s.end(), '0') - s.begin();
    auto x = z % 3, y = z / 3;
    int dxs[] = { 1, -1, 0, 0 }, dys[] = { 0, 0, 1, -1 };
    for (size_t i = 0; i < 4; i++) {
      int a = x + dxs[i], b = y + dys[i];
      if (0 <= a && a < 3 && 0 <= b && b < 3) {
        std::swap(s[3*y+x], s[3*b+a]);
        if (ss.find(s) == ss.end()) {
          ss[s] = d + 1;
          q.push(std::make_pair(s, d + 1));
        }
        std::swap(s[3*y+x], s[3*b+a]);
      }
    }
  }
  std::string s;
  for (size_t i = 0; i < 9; i++) {
    int c;
    scanf("%d", &c);
    s.push_back('0' + c);
  }
  printf("%d\n", ss[s]);
  return 0;
}
### Prompt

Construct a cpp code solution to the problem outlined:

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 <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <unordered_map>

int main() {
  std::unordered_map<std::string, int> ss;
  std::queue<std::pair<std::string, int>> q;
  q.push(std::make_pair("123456780", 0));
  ss["123456780"] = 0;
  while (q.size()) {
    auto p = q.front();
    q.pop();
    auto s = p.first;
    auto d = p.second;
    auto z = std::find(s.begin(), s.end(), '0') - s.begin();
    auto x = z % 3, y = z / 3;
    int dxs[] = { 1, -1, 0, 0 }, dys[] = { 0, 0, 1, -1 };
    for (size_t i = 0; i < 4; i++) {
      int a = x + dxs[i], b = y + dys[i];
      if (0 <= a && a < 3 && 0 <= b && b < 3) {
        std::swap(s[3*y+x], s[3*b+a]);
        if (ss.find(s) == ss.end()) {
          ss[s] = d + 1;
          q.push(std::make_pair(s, d + 1));
        }
        std::swap(s[3*y+x], s[3*b+a]);
      }
    }
  }
  std::string s;
  for (size_t i = 0; i < 9; i++) {
    int c;
    scanf("%d", &c);
    s.push_back('0' + c);
  }
  printf("%d\n", ss[s]);
  return 0;
}
```