#include<bits/stdc++.h>
using namespace std;

const int H=3,W=3;
const int SIZE=H*W;

class State
{
 public:
    string str;
    int space,distFromS,distToG;
    State(string str,int space,int dist):str(str),space(space),distFromS(dist),distToG(0){
        for(int i=0;i<SIZE;i++) if(i!=space){
            int y=i/W,x=i%W;
            int gy=(str[i]-'a')/W,gx=(str[i]-'a')%W;
            distToG+=abs(y-gy)+abs(x-gx);
        }
    }
    bool operator<(const State& s) const{
        return distFromS+distToG>s.distFromS+s.distToG;
    }
};

int d[4]={-W,-1,1,W};

int main()
{  
    string s(SIZE,' '),goal(SIZE,' ');
    for(int i=0;i<SIZE;i++) goal[i]=(char)('a'+i);
    for(int i=0;i<SIZE;i++){
        int t; cin>>t;
        if(t==0) s[i]=goal[SIZE-1];
        else s[i]=goal[t-1];
    }

    State start(s,s.find(goal[SIZE-1]),0);
    priority_queue<State> Q; Q.push(start);
    set<string> visited; visited.insert(s);
    while(!Q.empty()){
        auto cur=Q.top(); Q.pop();
        if(cur.str==goal){
            cout<<cur.distFromS<<endl;
            return 0;
        }
        for(int i=0;i<4;i++){
            int nxtspace=cur.space+d[i];
            if(nxtspace<0 or nxtspace>=SIZE) continue;
            if(d[i]==1 and nxtspace%W==0) continue;
            if(d[i]==-1 and nxtspace%W==W-1) continue;
            string nxtstr=cur.str;
            swap(nxtstr[cur.space],nxtstr[nxtspace]);
            if(!visited.count(nxtstr)){
                visited.insert(nxtstr);
                State nxt(nxtstr,nxtspace,cur.distFromS+1);
                Q.push(nxt);
            }
        }
    }
    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<bits/stdc++.h>
using namespace std;

const int H=3,W=3;
const int SIZE=H*W;

class State
{
 public:
    string str;
    int space,distFromS,distToG;
    State(string str,int space,int dist):str(str),space(space),distFromS(dist),distToG(0){
        for(int i=0;i<SIZE;i++) if(i!=space){
            int y=i/W,x=i%W;
            int gy=(str[i]-'a')/W,gx=(str[i]-'a')%W;
            distToG+=abs(y-gy)+abs(x-gx);
        }
    }
    bool operator<(const State& s) const{
        return distFromS+distToG>s.distFromS+s.distToG;
    }
};

int d[4]={-W,-1,1,W};

int main()
{  
    string s(SIZE,' '),goal(SIZE,' ');
    for(int i=0;i<SIZE;i++) goal[i]=(char)('a'+i);
    for(int i=0;i<SIZE;i++){
        int t; cin>>t;
        if(t==0) s[i]=goal[SIZE-1];
        else s[i]=goal[t-1];
    }

    State start(s,s.find(goal[SIZE-1]),0);
    priority_queue<State> Q; Q.push(start);
    set<string> visited; visited.insert(s);
    while(!Q.empty()){
        auto cur=Q.top(); Q.pop();
        if(cur.str==goal){
            cout<<cur.distFromS<<endl;
            return 0;
        }
        for(int i=0;i<4;i++){
            int nxtspace=cur.space+d[i];
            if(nxtspace<0 or nxtspace>=SIZE) continue;
            if(d[i]==1 and nxtspace%W==0) continue;
            if(d[i]==-1 and nxtspace%W==W-1) continue;
            string nxtstr=cur.str;
            swap(nxtstr[cur.space],nxtstr[nxtspace]);
            if(!visited.count(nxtstr)){
                visited.insert(nxtstr);
                State nxt(nxtstr,nxtspace,cur.distFromS+1);
                Q.push(nxt);
            }
        }
    }
    return 0;
}

```