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

const int N = 3;
const int LIMIT = 100;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
int limit;
int puz[N*N];
int zero;
int esti;
int MDT[N*N][N*N];

int dfs(int dep, int pre) {
	if ( esti == 0 ) return dep;
	if ( dep+esti >= limit ) return LIMIT;	

	int ret = LIMIT;
	for ( int i = 0; i < 4; i++ ) {
		if ( (i+2)%4 == pre ) continue;
		int y = zero/N, x = zero%N;
		int ny = y+dy[i], nx = x+dx[i];
		if ( ny < 0 || nx < 0 || ny >= N || nx >= N ) continue;
		int tmp[N*N];
		int tmp_zero  = zero;
		int tmp_esti = esti;		
		for ( int j = 0; j < N*N; j++ ) tmp[j] = puz[j];
		int ch_id = ny*N+nx;
		int ch_dat = puz[ch_id];		
		esti -= MDT[zero][N*N-1];
		esti -= MDT[ny*N+nx][ch_dat];
		esti += MDT[zero][ch_dat];
		esti += MDT[ny*N+nx][N*N-1];
		swap(puz[zero], puz[ch_id]);
		zero = ch_id;		
		ret = min(ret, dfs(dep+1, i));
		for ( int j = 0; j < N*N; j++ ) puz[j] = tmp[j];
		zero = tmp_zero;
		esti = tmp_esti;		
	}

	return ret;
}

int main() {
	for ( int i = 0; i < N*N; i++ ) {
		for ( int j = 0; j < N*N; j++ ) {
			MDT[i][j] = abs(i/N-j/N) + abs(i%N-j%N);            
		}
	}

	for ( int i = 0; i < N*N; i++ ) {		
		cin >> puz[i];
		if ( puz[i] == 0 ) {
			zero = i;
			puz[i] = N*N-1;			
		} else {
			puz[i]--;
		}
		esti += abs(i/N-puz[i]/N) + abs(i%N-puz[i]%N);        
	}

	for ( int i = esti; i <= LIMIT; i++ ) {
		limit = i;
		int ans = dfs(0, -1);
		if ( ans < 100 ) {
			cout << ans << endl;
			break;
		}
	}
	
	return 0;
}


### 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;

const int N = 3;
const int LIMIT = 100;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
int limit;
int puz[N*N];
int zero;
int esti;
int MDT[N*N][N*N];

int dfs(int dep, int pre) {
	if ( esti == 0 ) return dep;
	if ( dep+esti >= limit ) return LIMIT;	

	int ret = LIMIT;
	for ( int i = 0; i < 4; i++ ) {
		if ( (i+2)%4 == pre ) continue;
		int y = zero/N, x = zero%N;
		int ny = y+dy[i], nx = x+dx[i];
		if ( ny < 0 || nx < 0 || ny >= N || nx >= N ) continue;
		int tmp[N*N];
		int tmp_zero  = zero;
		int tmp_esti = esti;		
		for ( int j = 0; j < N*N; j++ ) tmp[j] = puz[j];
		int ch_id = ny*N+nx;
		int ch_dat = puz[ch_id];		
		esti -= MDT[zero][N*N-1];
		esti -= MDT[ny*N+nx][ch_dat];
		esti += MDT[zero][ch_dat];
		esti += MDT[ny*N+nx][N*N-1];
		swap(puz[zero], puz[ch_id]);
		zero = ch_id;		
		ret = min(ret, dfs(dep+1, i));
		for ( int j = 0; j < N*N; j++ ) puz[j] = tmp[j];
		zero = tmp_zero;
		esti = tmp_esti;		
	}

	return ret;
}

int main() {
	for ( int i = 0; i < N*N; i++ ) {
		for ( int j = 0; j < N*N; j++ ) {
			MDT[i][j] = abs(i/N-j/N) + abs(i%N-j%N);            
		}
	}

	for ( int i = 0; i < N*N; i++ ) {		
		cin >> puz[i];
		if ( puz[i] == 0 ) {
			zero = i;
			puz[i] = N*N-1;			
		} else {
			puz[i]--;
		}
		esti += abs(i/N-puz[i]/N) + abs(i%N-puz[i]%N);        
	}

	for ( int i = esti; i <= LIMIT; i++ ) {
		limit = i;
		int ans = dfs(0, -1);
		if ( ans < 100 ) {
			cout << ans << endl;
			break;
		}
	}
	
	return 0;
}


```