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

#define N 3
#define N2 9

struct Puzzle {
	int f[N2];
	int space;
	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;
	}
};

bool isTarget( Puzzle &u )
{
	for( int i = 0; i < N2; i++ ) {
		if( u.f[i] != i + 1 ) return false;
	}
	return true;
}

int main()
{
	Puzzle s;
	for( int i = 0; i < N2; i++ ) {
		cin >> s.f[i];
		if( s.f[i] == 0 ) {
			s.space = i;
			s.f[i] = N2;
		}
	}

	int dirh[] = { 0, 0, -1, 1 };
	int dirv[] = { -1, 1, 0, 0 };
	queue<pair<Puzzle, int>> que;
	map<Puzzle, int> m;
	int ans;
	que.push( make_pair( s, 0 ) );
	m[s] = 1;
	while( !que.empty() ) {
		Puzzle u;
		int cnt;
		tie( u, cnt ) = que.front();
		que.pop();
		if( isTarget( u ) ) {
			ans = cnt;
			break;
		}
		int sy = u.space / N;
		int sx = u.space % N;
		for( int i = 0; i < 4; i++ ) {
			int nx = sx + dirh[i];
			int ny = sy + dirv[i];
			if( nx < 0 || N <= nx || ny < 0 || N <= ny ) continue;
			int ni = ny * N + nx;
			Puzzle v;
			v = u;
			swap( v.f[ni], v.f[v.space] );
			v.space = ni;
			if( m.find( v ) == m.end() ) {
				que.push( make_pair( v, cnt + 1 ) );
				m[v] = 1;
			}
		}
	}

	cout << ans << endl;
}


### Prompt

Please formulate a CPP solution to 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;

#define N 3
#define N2 9

struct Puzzle {
	int f[N2];
	int space;
	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;
	}
};

bool isTarget( Puzzle &u )
{
	for( int i = 0; i < N2; i++ ) {
		if( u.f[i] != i + 1 ) return false;
	}
	return true;
}

int main()
{
	Puzzle s;
	for( int i = 0; i < N2; i++ ) {
		cin >> s.f[i];
		if( s.f[i] == 0 ) {
			s.space = i;
			s.f[i] = N2;
		}
	}

	int dirh[] = { 0, 0, -1, 1 };
	int dirv[] = { -1, 1, 0, 0 };
	queue<pair<Puzzle, int>> que;
	map<Puzzle, int> m;
	int ans;
	que.push( make_pair( s, 0 ) );
	m[s] = 1;
	while( !que.empty() ) {
		Puzzle u;
		int cnt;
		tie( u, cnt ) = que.front();
		que.pop();
		if( isTarget( u ) ) {
			ans = cnt;
			break;
		}
		int sy = u.space / N;
		int sx = u.space % N;
		for( int i = 0; i < 4; i++ ) {
			int nx = sx + dirh[i];
			int ny = sy + dirv[i];
			if( nx < 0 || N <= nx || ny < 0 || N <= ny ) continue;
			int ni = ny * N + nx;
			Puzzle v;
			v = u;
			swap( v.f[ni], v.f[v.space] );
			v.space = ni;
			if( m.find( v ) == m.end() ) {
				que.push( make_pair( v, cnt + 1 ) );
				m[v] = 1;
			}
		}
	}

	cout << ans << endl;
}


```