You stumbled upon a new kind of chess puzzles. The chessboard you are given is not necesserily $8 \times 8$, but it still is $N \times N$. Each square has some number written on it, all the numbers are from $1$ to $N^2$ and all the numbers are pairwise distinct. The $j$-th square in the $i$-th row has a number $A_{ij}$ written on it.

In your chess set you have only three pieces: a knight, a bishop and a rook. At first, you put one of them on the square with the number $1$ (you can choose which one). Then you want to reach square $2$ (possibly passing through some other squares in process), then square $3$ and so on until you reach square $N^2$. In one step you are allowed to either make a valid move with the current piece or replace it with some other piece. Each square can be visited arbitrary number of times.

A knight can move to a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. A bishop moves diagonally. A rook moves horizontally or vertically. The move should be performed to a different square from the one a piece is currently standing on.

You want to minimize the number of steps of the whole traversal. Among all the paths to have the same number of steps you want to choose the one with the lowest number of piece replacements.

What is the path you should take to satisfy all conditions?


-----Input-----

The first line contains a single integer $N$ ($3 \le N \le 10$) — the size of the chessboard.

Each of the next $N$ lines contains $N$ integers $A_{i1}, A_{i2}, \dots, A_{iN}$ ($1 \le A_{ij} \le N^2$) — the numbers written on the squares of the $i$-th row of the board.

It is guaranteed that all $A_{ij}$ are pairwise distinct.


-----Output-----

The only line should contain two integers — the number of steps in the best answer and the number of replacement moves in it.


-----Example-----
Input
3
1 9 3
8 6 7
4 2 5

Output
12 1



-----Note-----

Here are the steps for the first example (the starting piece is a knight):  Move to $(3, 2)$  Move to $(1, 3)$  Move to $(3, 2)$  Replace the knight with a rook  Move to $(3, 1)$  Move to $(3, 3)$  Move to $(3, 2)$  Move to $(2, 2)$  Move to $(2, 3)$  Move to $(2, 1)$  Move to $(1, 1)$  Move to $(1, 2)$

Difficulty: HARD
Tags: Dynamic programming

Graph traversal

Shortest paths
Skill types: Dynamic programming

Starter code:

Solutions:
n = int(input())
graph = [{}, {}, {}]
for i in range(n):
	for j in range(n):
		graph[0][i, j] = [(k, j) for k in range(n)] + [(i, k) for k in range(n)]
		graph[0][i, j].remove((i, j))
		graph[0][i, j].remove((i, j))
		graph[1][i, j] = []
		for k in range(n):
			for l in range(n):
				if abs(k - i) == abs(l - j) != 0:
					graph[1][i, j].append((k, l))
		graph[2][i, j] = []
		for k in range(n):
			for l in range(n):
				if {abs(k - i), abs(l - j)} == {1, 2}:
					graph[2][i, j].append((k, l))
dists = [[{}, {}, {}], [{}, {}, {}], [{}, {}, {}]]
for i in range(n):
	for j in range(n):
		for k in range(3):
			dists[k][k][i, j, i, j] = 0
for i in range(n):
	for j in range(n):
		for k in range(3):
			layers = [[(i, j, k, 0)], [], [], [], []]
			for l in range(4):
				for guy in layers[l]:
					for m in range(3):
						if m != guy[2]:
							if (i, j, guy[0], guy[1]) not in dists[k][m]:
								layers[l + 1].append((guy[0], guy[1], m, guy[3] + 1))
								dists[k][m][i, j, guy[0], guy[1]] = 1000 * (l + 1) + guy[3] + 1
					for boi in graph[guy[2]][guy[0], guy[1]]:
						if (i, j, boi[0], boi[1]) not in dists[k][guy[2]]:
							layers[l + 1].append((boi[0], boi[1], guy[2], guy[3]))
							dists[k][guy[2]][i, j, boi[0], boi[1]] = 1000 * (l + 1) + guy[3]
						elif 1000 * (l + 1) + guy[3] < dists[k][guy[2]][i, j, boi[0], boi[1]]:
							layers[l + 1].append((boi[0], boi[1], guy[2], guy[3]))
							dists[k][guy[2]][i, j, boi[0], boi[1]] = 1000 * (l + 1) + guy[3]
locs = [None] * n ** 2
for i in range(n):
	a = list(map(int, input().split()))
	for j in range(n):
		locs[a[j] - 1] = (i, j)
best = (0, 0, 0)
for i in range(n ** 2 - 1):
	tup = (locs[i][0], locs[i][1], locs[i + 1][0], locs[i + 1][1])
	new0 = min(best[0] + dists[0][0][tup], best[1] + dists[1][0][tup], best[2] + dists[2][0][tup])
	new1 = min(best[0] + dists[0][1][tup], best[1] + dists[1][1][tup], best[2] + dists[2][1][tup])
	new2 = min(best[0] + dists[0][2][tup], best[1] + dists[1][2][tup], best[2] + dists[2][2][tup])
	best = (new0, new1, new2)
a = min(best)
print(a // 1000, a % 1000)


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()
n = int(minp())
m = [None] * n
k = [None] * 3
dp = [None] * 3
dp[0] = [None] * (n * n)
dp[1] = [None] * (n * n)
dp[2] = [None] * (n * n)
path = [None] * (n * n)
for i in range(n):
	m[i] = list(map(int, minp().split()))
	for j in range(n):
		path[m[i][j] - 1] = (i, j)
for z in range(3):
	k_ = [None] * n
	for i in range(n):
		kk = [None] * n
		for j in range(n):
			kkk_ = [None] * 3
			for zz in range(3):
				kkk = [None] * n
				for w in range(n):
					kkk[w] = [(1000000, 0)] * n
				kkk_[zz] = kkk
			kk[j] = kkk_
		k_[i] = kk
	k[z] = k_
q = [0] * (10 * n * n)
qr = 0
km = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
sm = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
lm = [(0, 1), (0, -1), (-1, 0), (1, 0)]
mm = [km, sm, lm]
for z in range(3):
	for i in range(n):
		for j in range(n):
			ql = 0
			qr = 1
			q[0] = (z, i, j, (0, 0))
			kc = k[z][i][j]
			kc[z][i][j] = (0, 0)
			while ql < qr:
				(t, x, y, dd) = q[ql]
				d = kc[t][x][y]
				ql += 1
				if d != dd:
					continue
				dd = (d[0] + 1, d[1] + 1)
				for tt in range(3):
					if t != tt and kc[tt][x][y] > dd:
						kc[tt][x][y] = dd
						q[qr] = (tt, x, y, dd)
						qr += 1
				dd = (d[0] + 1, d[1])
				if t == 0:
					for w in mm[t]:
						(xx, yy) = (w[0] + x, w[1] + y)
						if xx >= 0 and xx < n and (yy >= 0) and (yy < n):
							if kc[t][xx][yy] > dd:
								kc[t][xx][yy] = dd
								q[qr] = (t, xx, yy, dd)
								qr += 1
				else:
					for w in mm[t]:
						for hm in range(n * 2):
							(xx, yy) = (w[0] * hm + x, w[1] * hm + y)
							if xx >= 0 and xx < n and (yy >= 0) and (yy < n):
								if kc[t][xx][yy] > dd:
									kc[t][xx][yy] = dd
									q[qr] = (t, xx, yy, dd)
									qr += 1
							else:
								break
dp[0][0] = (0, 0)
dp[1][0] = (0, 0)
dp[2][0] = (0, 0)
for i in range(0, n * n - 1):
	(x, y) = path[i]
	(xx, yy) = path[i + 1]
	for z in range(3):
		for j in range(3):
			dist = k[j][x][y][z][xx][yy]
			if dp[j][i] != None:
				nd = (dp[j][i][0] + dist[0], dp[j][i][1] + dist[1])
				if dp[z][i + 1] == None:
					dp[z][i + 1] = nd
				else:
					dp[z][i + 1] = min(dp[z][i + 1], nd)
for j in range(n * n - 1, n * n):
	qq = [dp[i][j] if dp[i][j] != None else (1000000, 0) for i in range(3)]
	qm = min(qq)
	print(qm[0], qm[1])