#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#include <map>
#define NN 9
using namespace std;
typedef long long ll;
typedef pair<vector<int>, int> P;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
vector<int> start(NN);
vector<int> goal(NN);
int bfs()
{
    queue<P> que;
    map<vector<int>, int> mp;
    que.push(P(start, 0));
    mp[start] = 0;
    while(que.size())
    {
        P p = que.front();
        que.pop();
        vector<int> now = p.first;
        int res = p.second;
        if(now == goal) return res;
        int y, x, pos;
        for(int i = 0; i < NN; i++)
        {
            if(now[i] != 0) continue;
            x = i % 3;
            y = i / 3;
            pos = i;
        }
        res++;
        for(int i = 0; i < 4; i++)
        {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 || nx < 0 || ny >= 3 || nx >= 3) continue;
            int idx = 3 * ny + nx;
            vector<int> next = now;
            swap(next[pos], next[idx]);
            if(mp.count(next) == 0 || mp[next] > res)
            {
                mp[next] = res;
                que.push(P(next, res));
            }
        }
    }
    return -1;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    for(int i = 0; i < NN; i++) 
    {
        cin >> start[i];
        goal[i] = (i + 1) % NN;
    }
    cout << bfs() << endl;
    return 0;
}



### Prompt

In Cpp, your task is to solve 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 <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#include <map>
#define NN 9
using namespace std;
typedef long long ll;
typedef pair<vector<int>, int> P;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
vector<int> start(NN);
vector<int> goal(NN);
int bfs()
{
    queue<P> que;
    map<vector<int>, int> mp;
    que.push(P(start, 0));
    mp[start] = 0;
    while(que.size())
    {
        P p = que.front();
        que.pop();
        vector<int> now = p.first;
        int res = p.second;
        if(now == goal) return res;
        int y, x, pos;
        for(int i = 0; i < NN; i++)
        {
            if(now[i] != 0) continue;
            x = i % 3;
            y = i / 3;
            pos = i;
        }
        res++;
        for(int i = 0; i < 4; i++)
        {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 || nx < 0 || ny >= 3 || nx >= 3) continue;
            int idx = 3 * ny + nx;
            vector<int> next = now;
            swap(next[pos], next[idx]);
            if(mp.count(next) == 0 || mp[next] > res)
            {
                mp[next] = res;
                que.push(P(next, res));
            }
        }
    }
    return -1;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    for(int i = 0; i < NN; i++) 
    {
        cin >> start[i];
        goal[i] = (i + 1) % NN;
    }
    cout << bfs() << endl;
    return 0;
}



```