#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;

class puzzle8 {
public:
	int** field;
	int cost;
	int guess;
	int row_0, column_0;
	puzzle8();
	puzzle8(puzzle8*);
	void calc_guess();
	void slide_up();
	void slide_down();
	void slide_left();
	void slide_right();
};



puzzle8::puzzle8() {
	field = new int* [3];
	for (int i = 0;i < 3;i++) {
		field[i] = new int[3];
	}
	column_0 = row_0 = 0;
	cost = guess = 0;
}
puzzle8::puzzle8(puzzle8* a) {
	field = new int* [3];
	for (int i = 0;i < 3;i++) {
		field[i] = new int[3];
		for (int j = 0;j < 3;j++) {
			field[i][j] = a->field[i][j];

		}
	}
	cost = a->cost;
	guess = a->guess;
	row_0 = a->row_0;
	column_0 = a->column_0;
}
void puzzle8::calc_guess() {
	guess = 0;
	for (int i = 0;i < 3;i++) {
		for (int j = 0;j < 3;j++) {
			if (field[i][j] != 8) {
				guess += abs(i - field[i][j] / 3) + abs(j - field[i][j] % 3);
			}	
		}
	}
}
void puzzle8::slide_up() {
	swap(field[row_0][column_0], field[row_0 - 1][column_0]);
	guess += abs(field[row_0][column_0] / 3 - row_0) - abs(field[row_0][column_0] / 3 - row_0 + 1);
	row_0--;
	cost++;
}
void puzzle8::slide_down() {
	swap(field[row_0][column_0], field[row_0 + 1][column_0]);
	guess += abs(field[row_0][column_0] / 3 - row_0) - abs(field[row_0][column_0] / 3 - row_0 - 1);
	row_0++;
	cost++;
}
void puzzle8::slide_left() {
	swap(field[row_0][column_0], field[row_0][column_0 - 1]);
	guess += abs(field[row_0][column_0] % 3 - column_0) - abs(field[row_0][column_0] % 3 - column_0 + 1);
	column_0--;
	cost++;
}
void puzzle8::slide_right() {
	swap(field[row_0][column_0], field[row_0][column_0 + 1]);
	guess += abs(field[row_0][column_0] % 3 - column_0) - abs(field[row_0][column_0] % 3 - column_0 - 1);
	column_0++;
	cost++;
}

int btoi(puzzle8* a) {
	int sum = 0;
	for (int i = 0;i < 9;i++) {
		sum += pow(9, i) * a->field[i / 3][i % 3];
	}
	return sum;
}

inline bool operator>(puzzle8 a, puzzle8 b) {
	return a.cost + a.guess > b.cost + b.guess;
}
inline bool operator>=(puzzle8 a, puzzle8 b) {
	return a.cost + a.guess >= b.cost + b.guess;
}
inline bool operator<(puzzle8 a, puzzle8 b) {
	return a.cost + a.guess < b.cost + b.guess;
}
inline bool operator<=(puzzle8 a, puzzle8 b) {
	return a.cost + a.guess <= b.cost + b.guess;
}

int main() {
	puzzle8 p,new_puzzle;
	
	priority_queue<puzzle8,vector<puzzle8>,greater<puzzle8>> pq;
	vector<int> appeared;
	int tmp;
	for (int i = 0;i < 3;i++) {
		for (int j = 0;j < 3;j++) {
			cin >> p.field[i][j];
			if (p.field[i][j] == 0) {
				p.field[i][j] = 8;
				p.row_0 = i;
				p.column_0 = j;
			}
			else p.field[i][j]--;
		}
	}
	p.cost = 0;
	p.calc_guess();
	appeared.push_back(btoi(&p));
	while (p.guess != 0) {
		if (p.row_0 != 0) {
			new_puzzle = new puzzle8(&p);
			new_puzzle.slide_up();
			tmp = btoi(&new_puzzle);
			if (find(appeared.begin(), appeared.end(), tmp) == appeared.end()) {
				pq.push(new_puzzle);
				appeared.push_back(tmp);
			}	
		}
		if (p.row_0 != 2) {
			new_puzzle = new puzzle8(&p);
			new_puzzle.slide_down();
			tmp = btoi(&new_puzzle);
			if (find(appeared.begin(), appeared.end(), tmp) == appeared.end()) {
				pq.push(new_puzzle);
				appeared.push_back(tmp);
			}
		}
		if (p.column_0 != 0) {
			new_puzzle = new puzzle8(&p);
			new_puzzle.slide_left();
			tmp = btoi(&new_puzzle);
			if (find(appeared.begin(), appeared.end(), tmp) == appeared.end()) {
				pq.push(new_puzzle);
				appeared.push_back(tmp);
			}
		}
		if (p.column_0 != 2) {
			new_puzzle = new puzzle8(&p);
			new_puzzle.slide_right();
			tmp = btoi(&new_puzzle);
			if (find(appeared.begin(), appeared.end(), tmp) == appeared.end()) {
				pq.push(new_puzzle);
				appeared.push_back(tmp);
			}
		}
		p = pq.top();
		pq.pop();
	}
	cout << p.cost << endl;
}


Measured memory_bytes: 9236