#include <iostream>
#include <vector>
#include <queue>
#include <map>
#define Size 9

using namespace std; 

int find_zero(string state) {
	for (int i = 0; i < Size; ++i) {
		if (state[i] == '0') {
			return i;
		}
	}
	return -1;
}

string move(string str, int p1, int p2) {
	string res = str;
	res[p1] = str[p2];
	res[p2] = str[p1];
	return res;
}

int main(void) {
	string state = "012345678";
	for (int i = 0; i < Size; ++i) {
		cin >> state[i];
	}

	int zi;
	map<string,int> memo;
	memo[state] = 1;
	pair<string,int> now;
	queue< pair<string,int> > q;
	q.push({state,1});
	while (!q.empty()) {
		now = q.front();
		q.pop();
		if (now.first == "123456780") {
			cout << now.second-1 << endl;
			break;
		}
		zi = find_zero(now.first);
		if (zi%3 != 2) {
			state = move(now.first,zi,zi+1);
			if (memo[state] == 0) {
				memo[state] = now.second+1;
				q.push({state,now.second+1});
			}
		}
		if (zi%3 != 0) {
			state = move(now.first,zi,zi-1);
			if (memo[state] == 0) {
				memo[state] = now.second+1;
				q.push({state,now.second+1});
			}
		}
		if (zi/3 > 0) {
			state = move(now.first,zi,zi-3);
			if (memo[state] == 0) {
				memo[state] = now.second+1;
				q.push({state,now.second+1});
			}
		}
		if (zi/3 < 2) {
			state = move(now.first,zi,zi+3);
			if (memo[state] == 0) {
				memo[state] = now.second+1;
				q.push({state,now.second+1});
			}
		}
	}
	return 0;
}


### Prompt

Your challenge is to write 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 <iostream>
#include <vector>
#include <queue>
#include <map>
#define Size 9

using namespace std; 

int find_zero(string state) {
	for (int i = 0; i < Size; ++i) {
		if (state[i] == '0') {
			return i;
		}
	}
	return -1;
}

string move(string str, int p1, int p2) {
	string res = str;
	res[p1] = str[p2];
	res[p2] = str[p1];
	return res;
}

int main(void) {
	string state = "012345678";
	for (int i = 0; i < Size; ++i) {
		cin >> state[i];
	}

	int zi;
	map<string,int> memo;
	memo[state] = 1;
	pair<string,int> now;
	queue< pair<string,int> > q;
	q.push({state,1});
	while (!q.empty()) {
		now = q.front();
		q.pop();
		if (now.first == "123456780") {
			cout << now.second-1 << endl;
			break;
		}
		zi = find_zero(now.first);
		if (zi%3 != 2) {
			state = move(now.first,zi,zi+1);
			if (memo[state] == 0) {
				memo[state] = now.second+1;
				q.push({state,now.second+1});
			}
		}
		if (zi%3 != 0) {
			state = move(now.first,zi,zi-1);
			if (memo[state] == 0) {
				memo[state] = now.second+1;
				q.push({state,now.second+1});
			}
		}
		if (zi/3 > 0) {
			state = move(now.first,zi,zi-3);
			if (memo[state] == 0) {
				memo[state] = now.second+1;
				q.push({state,now.second+1});
			}
		}
		if (zi/3 < 2) {
			state = move(now.first,zi,zi+3);
			if (memo[state] == 0) {
				memo[state] = now.second+1;
				q.push({state,now.second+1});
			}
		}
	}
	return 0;
}


```