#include<bits/stdc++.h>

using namespace std;

map<string,int> mp;

int dr[4]={-1,1,0,0},dc[4]={0,0,-1,1};

void bfs()
{
    queue<string> q;
    mp["123456780"]=0;
    q.push("123456780");

    while(!q.empty())
    {
        string s=q.front();q.pop();
        //cout<<s<<" "<<mp[s]<<endl;
        int idx;
        for(idx=0;idx<s.length()&&s[idx]!='0';idx++);

        int r=idx/3,c=idx%3;
        for(int i=0;i<4;i++)
        {
            int rr=r+dr[i],cc=c+dc[i];

            if(rr<0||rr>=3||cc<0||cc>=3) continue;

            int idx1=rr*3+cc;
            string s1=s;
            swap(s1[idx],s1[idx1]);
            if(mp.find(s1)==mp.end())
            {
                mp[s1]=mp[s]+1;
                q.push(s1);
            }
        }
    }
}

char mat[3][3];

int main()
{
    bfs();

    string s;

    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            char ch;
            scanf("%c%c",&mat[i][j],&ch);
            s+=mat[i][j];
        }
    }

    printf("%d\n",mp[s]);

    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<bits/stdc++.h>

using namespace std;

map<string,int> mp;

int dr[4]={-1,1,0,0},dc[4]={0,0,-1,1};

void bfs()
{
    queue<string> q;
    mp["123456780"]=0;
    q.push("123456780");

    while(!q.empty())
    {
        string s=q.front();q.pop();
        //cout<<s<<" "<<mp[s]<<endl;
        int idx;
        for(idx=0;idx<s.length()&&s[idx]!='0';idx++);

        int r=idx/3,c=idx%3;
        for(int i=0;i<4;i++)
        {
            int rr=r+dr[i],cc=c+dc[i];

            if(rr<0||rr>=3||cc<0||cc>=3) continue;

            int idx1=rr*3+cc;
            string s1=s;
            swap(s1[idx],s1[idx1]);
            if(mp.find(s1)==mp.end())
            {
                mp[s1]=mp[s]+1;
                q.push(s1);
            }
        }
    }
}

char mat[3][3];

int main()
{
    bfs();

    string s;

    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            char ch;
            scanf("%c%c",&mat[i][j],&ch);
            s+=mat[i][j];
        }
    }

    printf("%d\n",mp[s]);

    return 0;
}


```