#include <iostream>
#include <cstring>
#include <map>
#include <queue>
using namespace std;

const int N = 3;
const int N2 = 9;
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
//string dir[5] = {"r", "l", "d", "u"};//右、左、下、上
char dir[5] = {'r', 'l', 'd', 'u'};//右、左、下、上
struct Puzzle
{
    int f[N2];
    int space;
    string path;

    bool operator < (const Puzzle &p) const {
        for(int i=0; i<N2; i++)
        {
            if(f[i]==p.f[i]) continue;
            return f[i] > p.f[i];
        }
        return false;
    }
};
Puzzle P;
bool isTarget(Puzzle s)
{
    for(int i=0; i<N2-1; i++)
    {
        if(s.f[i]!=i+1) return 0;
    }
    return 1;
}
int bfs(Puzzle P)
{
    map<Puzzle, bool> V;
    queue<Puzzle> Q;
    Q.push(P);
    V[P] = true;
    Puzzle u, v;
    while(!Q.empty())
    {
        u = Q.front();
        Q.pop();
        if(isTarget(u)) return u.path.size();
        int sx = u.space/N, sy = u.space%N;
        //cout << 1 << endl;
        for(int i=0; i<4; i++)
        {
            int tx = sx + dx[i], ty = sy + dy[i];
            if(tx>=N || tx<0 || ty>=N || ty<0) continue;
            v = u;
            swap(v.f[u.space], v.f[tx*N+ty]);
            v.space = tx*N+ty;
            if(!V[v])
            {
                V[v] = true;
                v.path += dir[i];
                Q.push(v);
            }
        }
    }
    return -1;
}
int main()
{
    for(int i=0; i<N2; i++)
    {
        cin >> P.f[i];
        if(P.f[i]==0)
        {
            P.space = i;
        }
    }
    P.path = "";
    cout << bfs(P) << 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 <iostream>
#include <cstring>
#include <map>
#include <queue>
using namespace std;

const int N = 3;
const int N2 = 9;
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
//string dir[5] = {"r", "l", "d", "u"};//右、左、下、上
char dir[5] = {'r', 'l', 'd', 'u'};//右、左、下、上
struct Puzzle
{
    int f[N2];
    int space;
    string path;

    bool operator < (const Puzzle &p) const {
        for(int i=0; i<N2; i++)
        {
            if(f[i]==p.f[i]) continue;
            return f[i] > p.f[i];
        }
        return false;
    }
};
Puzzle P;
bool isTarget(Puzzle s)
{
    for(int i=0; i<N2-1; i++)
    {
        if(s.f[i]!=i+1) return 0;
    }
    return 1;
}
int bfs(Puzzle P)
{
    map<Puzzle, bool> V;
    queue<Puzzle> Q;
    Q.push(P);
    V[P] = true;
    Puzzle u, v;
    while(!Q.empty())
    {
        u = Q.front();
        Q.pop();
        if(isTarget(u)) return u.path.size();
        int sx = u.space/N, sy = u.space%N;
        //cout << 1 << endl;
        for(int i=0; i<4; i++)
        {
            int tx = sx + dx[i], ty = sy + dy[i];
            if(tx>=N || tx<0 || ty>=N || ty<0) continue;
            v = u;
            swap(v.f[u.space], v.f[tx*N+ty]);
            v.space = tx*N+ty;
            if(!V[v])
            {
                V[v] = true;
                v.path += dir[i];
                Q.push(v);
            }
        }
    }
    return -1;
}
int main()
{
    for(int i=0; i<N2; i++)
    {
        cin >> P.f[i];
        if(P.f[i]==0)
        {
            P.space = i;
        }
    }
    P.path = "";
    cout << bfs(P) << endl;
    return 0;
}


```