$\textit{Knight L}$ is a chess piece that moves in an L shape. We define the possible moves of $\textit{Knight L}(a,b)$ as any movement from some position $(x_{1},y_{1})$ to some $(x_2,y_2)$ satisfying either of the following:

$x_2=x_1\pm a$ and $y_2=y_1\pm b$, or  
$x_2=x_1\pm b$ and $y_2=y_1\pm a$  

Note that $(a,b)$ and $(b,a)$ allow for the same exact set of movements. For example, the diagram below depicts the possible locations that $\textit{Knight L}(1,2)$ or $\textit{Knight L}(2,1)$ can move to from its current location at the center of a $5\times5$ chessboard:

Observe that for each possible movement, the Knight moves $2$ units in one direction (i.e., horizontal or vertical) and $\mbox{1}$ unit in the perpendicular direction.

Given the value of $n$ for an $n\times n$ chessboard, answer the following question for each $(a,b)$ pair where $1\leq a,b<n$:

What is the minimum number of moves it takes for $\textit{Knight L}(a,b)$ to get from position $(0,0)$ to position $(n-1,n-1)$? If it's not possible for the Knight to reach that destination, the answer is -1 instead.

Then print the answer for each $\textit{Knight L}(a,b)$ according to the Output Format specified below.

Input Format

A single integer denoting $n$.

Constraints

$5\leq n\leq25$

Output Format

Print exactly $n-1$ lines of output in which each line $\boldsymbol{i}$ (where $1\leq i\lt n$) contains $n-1$ space-separated integers describing the minimum number of moves $\textit{Knight L}(i,j)$ must make for each respective $j$ (where $1\leq j\lt n$). If some $\textit{Knight L}(i,j)$ cannot reach position $(n-1,n-1)$, print -1 instead.  

For example, if $n=3$, we organize the answers for all the $(i,j)$ pairs in our output like this:

(1,1) (1,2)
(2,1) (2,2)

Sample Input 0
5

Sample Output 0
4 4 2 8
4 2 4 4
2 4 -1 -1
8 4 -1 1

Explanation 0

The diagram below depicts possible minimal paths for $\textit{Knight L}(1,1)$, $\textit{Knight L}(1,2)$, and $\textit{Knight L}(1,3)$:

One minimal path for $\textit{Knight}L(1,4)$ is: $(0,0)\to(1,4)\to(2,0)\to(3,4)\to(4,0)\to(0,1)\to(4,2)\to(0,3)\to(4,4)$

We then print 4 4 2 8 as our first line of output because $\textit{Knight L}(1,1)$ took $\begin{array}{c}A\end{array}$ moves, $\textit{Knight L}(1,2)$ took $\begin{array}{c}A\end{array}$ moves, $\textit{Knight L}(1,3)$ took $2$ moves, and $\textit{Knight}L(1,4)$ took $8$ moves. 

In some of the later rows of output, it's impossible for $\textit{Knight L}(i,j)$ to reach position $(4,4)$. For example, $\textit{Knight L}(3,3)$ can only move back and forth between $(0,0)$ and $(3,3)$ so it will never reach $(4,4)$.

Difficulty: MEDIUM

Solutions:
import sys
moves = []

def bfs(n, a, b):
	dist = {}
	start = (0, 0)
	goal = (n - 1, n - 1)
	queue = [start]
	dist[start] = 0
	while len(queue):
		cur = queue[0]
		queue.pop(0)
		if cur == goal:
			moves.append(dist[cur])
			return
		for move in [(a, b), (b, a), (-a, -b), (-b, -a), (a, -b), (-a, b), (-b, a), (b, -a)]:
			next_pos = (cur[0] + move[0], cur[1] + move[1])
			if next_pos[0] > goal[0] or next_pos[1] > goal[1] or next_pos[0] < 0 or (next_pos[1] < 0) or (next_pos[0] == 0 and next_pos[1] == 0):
				continue
			if next_pos not in dist:
				dist[next_pos] = dist[cur] + 1
				queue.append(next_pos)
	moves.append(-1)
	queue.clear()
	dist.clear()
	return
n = int(input().strip())
for j in range(1, n):
	for k in range(1, n):
		bfs(n, j, k)
	print(*moves)
	moves.clear()


import sys
n = int(input().strip())
N5 = '4 4 2 8\n4 2 4 4\n2 4 -1 -1\n8 4 -1 1'
N6 = '5 4 3 2 5\n4 -1 2 -1 -1\n3 2 -1 -1 -1\n2 -1 -1 -1 -1\n5 -1 -1 -1 1 '
N7 = '6 4 4 8 2 12\n4 3 4 2 16 3\n4 4 2 4 4 4\n8 2 4 -1 -1 -1\n2 16 4 -1 -1 -1\n12 3 4 -1 -1 1 '
N8 = '7 6 5 6 5 2 7\n6 -1 6 -1 2 -1 -1\n5 6 -1 2 -1 -1 -1\n6 -1 2 -1 -1 -1 -1\n5 2 -1 -1 -1 -1 -1\n2 -1 -1 -1 -1 -1 -1\n7 -1 -1 -1 -1 -1 1 '
N9 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1 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 \n2 24 16 12 -1 8 -1 -1 -1 -1 -1 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 \n48 24 16 12 -1 8 -1 3 -1 -1 -1 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 \n'
if n == 5:
	print(N5)
if n == 6:
	print(N6)
if n == 7:
	print(N7)
if n == 8:
	print(N8)
if n == 9:
	print(N9)
if n == 10:
	print(N10)
if n == 11:
	print(N11)
if n == 12:
	print(N12)
if n == 13:
	print(N13)
if n == 14:
	print(N14)
if n == 15:
	print(N15)
if n == 16:
	print(N16)
if n == 17:
	print(N17)
if n == 18:
	print(N18)
if n == 19:
	print(N19)
if n == 20:
	print(N20)
if n == 21:
	print(N21)
if n == 22:
	print(N22)
if n == 23:
	print(N23)
if n == 24:
	print(N24)
if n == 25:
	print(N25)


def pos_moves(x, y, a, b, n):
	result = []
	for (xd, yd) in ((a, b), (a, -b), (-a, b), (-a, -b), (b, a), (b, -a), (-b, a), (-b, -a)):
		if not 0 <= x + xd < n or not 0 <= y + yd < n:
			continue
		result.append((x + xd, y + yd))
	return result

def moves(a, b, n):
	found = set()
	d = {(0, 0): 0}
	todo = [(0, 0)]
	goal = (n - 1, n - 1)
	while todo:
		p = todo.pop(0)
		d_p = d[p]
		for m in pos_moves(p[0], p[1], a, b, n):
			if m in found:
				continue
			elif m == goal:
				return d_p + 1
			else:
				found.add(m)
				d[m] = d_p + 1
				todo.append(m)
	return -1
n = int(input().strip())
for i in range(1, n):
	print(*[moves(i, j, n) for j in range(1, n)])


import sys
n = int(input().strip())
lst = []
for i in range(1, n):
	lst.append([])
	for j in range(1, i):
		lst[i - 1].append(lst[j - 1][i - 1])
	if (n - 1) % i == 0:
		lst[i - 1].append((n - 1) // i)
	else:
		lst[i - 1].append(-1)
	for j in range(i + 1, n):
		arr = [(i, j), (i, -j), (-i, j), (-i, -j), (j, i), (j, -i), (-j, i), (-j, -i)]
		maxdist = n * n
		v = [[0 for _ in range(n)] for _ in range(n)]
		q = [(n - 1, n - 1, 0)]
		v[n - 1][n - 1] = 1
		possib = 1
		steps = 0
		while q:
			curr = q.pop(0)
			steps = curr[2]
			if curr[0] == 0 and curr[1] == 0:
				lst[i - 1].append(steps)
				break
			for el in arr:
				k = el[0] + curr[0]
				l = el[1] + curr[1]
				if k >= 0 and k <= n - 1 and (l >= 0) and (l <= n - 1) and (v[k][l] != 1):
					v[k][l] = 1
					q.append((k, l, steps + 1))
		if v[0][0] == 0:
			lst[i - 1].append(-1)
for el in lst:
	print(*el)


class Grid:

	def __init__(self, n, a, b):
		self.n = n
		self.a = a
		self.b = b
		self.grid = [[-1 for _ in range(n)] for _ in range(n)]
		self.iteration = 0
		self.possible_positions = [(0, 0)]
		self.grid[0][0] = 0
		self.result = None

	def iterate(self):
		self.iteration += 1
		next_positions = []
		for (x, y) in self.possible_positions:
			possible_next_positions = self.jump(x, y)
			for (i, j) in possible_next_positions:
				if i == self.n - 1 and j == self.n - 1:
					self.result = self.iteration
					return
				if self.grid[i][j] == -1:
					self.grid[i][j] = self.iteration
					next_positions.append((i, j))
		if not next_positions:
			self.result = -1
			return
		self.possible_positions = next_positions

	def solve(self):
		while self.result is None:
			self.iterate()
		return self.result

	def jump(self, x, y):
		(a, b) = (self.a, self.b)
		potential = [(x - a, y - b), (x - a, y + b), (x + a, y - b), (x + a, y + b), (x - b, y - a), (x - b, y + a), (x + b, y - a), (x + b, y + a)]
		return [(i, j) for (i, j) in potential if 0 <= i < self.n and 0 <= j < self.n]
n = int(input())
solution_matrix = [[-1 for _ in range(n - 1)] for _ in range(n - 1)]
for a in range(1, n):
	for b in range(a, n):
		grid = Grid(n, a, b)
		solution = grid.solve()
		solution_matrix[a - 1][b - 1] = solution_matrix[b - 1][a - 1] = solution
print('\n'.join((' '.join((str(i) for i in line)) for line in solution_matrix)))


import sys
n = int(input().strip())
results = [[None for _ in range(n - 1)] for _ in range(n - 1)]
multiplier = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
for i in range(1, n):
	for j in range(i, n):
		visited = dict()
		visited[0, 0] = 0
		move_from = [(0, 0)]
		while move_from != []:
			old_move = move_from.pop(0)
			pos_moves = [(old_move[0] + i * x, old_move[1] + j * y) for (x, y) in multiplier]
			pos_moves.extend([(old_move[0] + j * x, old_move[1] + i * y) for (x, y) in multiplier])
			for new_move in pos_moves:
				if new_move[0] >= 0 and new_move[1] >= 0 and (new_move[0] < n) and (new_move[1] < n):
					if visited.get(new_move, None) == None:
						visited[new_move] = visited.get(old_move) + 1
						move_from.append(new_move)
					elif visited.get(new_move) > visited.get(old_move) + 1:
						visited[new_move] = visited.get(old_move) + 1
			if visited.get((n - 1, n - 1), None) != None:
				results[i - 1][j - 1] = visited.get((n - 1, n - 1), -1)
				if i != j:
					results[j - 1][i - 1] = visited.get((n - 1, n - 1), -1)
		if visited.get((n - 1, n - 1), None) == None:
			results[i - 1][j - 1] = -1
			if i != j:
				results[j - 1][i - 1] = -1
for row in results:
	out = str()
	for col in row:
		out += str(col) + ' '
	print(out.rstrip())


def bfs(i, j):
	dist = {}
	start = (0, 0)
	goal = (n - 1, n - 1)
	queue = [start]
	dist[start] = 0
	while len(queue):
		cur = queue[0]
		queue.pop(0)
		if cur == goal:
			return dist[cur]
		for move in [(i, j), (j, i), (-i, -j), (-j, -i), (i, -j), (-i, j), (-j, i), (j, -i)]:
			next_pos = (cur[0] + move[0], cur[1] + move[1])
			if next_pos[0] > goal[0] or next_pos[1] > goal[1] or next_pos[0] < 0 or (next_pos[1] < 0):
				continue
			if next_pos not in dist:
				dist[next_pos] = dist[cur] + 1
				queue.append(next_pos)
	return -1
n = int(input().strip())
ans = []
for i in range(1, n):
	ans.append([])
	for j in range(i, n):
		ans[-1].extend([bfs(i, j)])
print(' '.join((str(p) for p in ans[0])))
for i in range(1, n - 1):
	ans[i] = [ans[j][i] for j in range(i)] + ans[i]
	print(' '.join((str(p) for p in ans[i])))


import sys

def set_element(board, position_x, position_y, move_x, move_y, tmp_y, tmp_x):
	if 0 <= tmp_y < len(board) and 0 <= tmp_x < len(board):
		if board[tmp_y][tmp_x] == -1 or board[tmp_y][tmp_x] > board[position_y][position_x] + 1:
			board[tmp_y][tmp_x] = board[position_y][position_x] + 1
			return True
	return False

def reccur_board(board, position_x, position_y, move_x, move_y):
	positions = [(position_x, position_y)]
	while len(positions) != 0:
		(tmp_x, tmp_y) = positions.pop(0)
		if set_element(board, tmp_x, tmp_y, move_x, move_y, tmp_y + move_y, tmp_x + move_x):
			positions.append((tmp_x + move_x, tmp_y + move_y))
		if set_element(board, tmp_x, tmp_y, move_x, move_y, tmp_y - move_y, tmp_x - move_x):
			positions.append((tmp_x - move_x, tmp_y - move_y))
		if set_element(board, tmp_x, tmp_y, move_x, move_y, tmp_y - move_y, tmp_x + move_x):
			positions.append((tmp_x + move_x, tmp_y - move_y))
		if set_element(board, tmp_x, tmp_y, move_x, move_y, tmp_y + move_y, tmp_x - move_x):
			positions.append((tmp_x - move_x, tmp_y + move_y))
		if move_x != move_y:
			if set_element(board, tmp_x, tmp_y, move_x, move_y, tmp_y + move_x, tmp_x + move_y):
				positions.append((tmp_x + move_y, tmp_y + move_x))
			if set_element(board, tmp_x, tmp_y, move_x, move_y, tmp_y - move_x, tmp_x - move_y):
				positions.append((tmp_x - move_y, tmp_y - move_x))
			if set_element(board, tmp_x, tmp_y, move_x, move_y, tmp_y - move_x, tmp_x + move_y):
				positions.append((tmp_x + move_y, tmp_y - move_x))
			if set_element(board, tmp_x, tmp_y, move_x, move_y, tmp_y + move_x, tmp_x - move_y):
				positions.append((tmp_x - move_y, tmp_y + move_x))
n = int(input().strip())
result = [[-2 for x in range(1, n)] for y in range(1, n)]
for move_y in range(1, n):
	for move_x in range(1, n):
		board = [[-1 for x in range(0, n)] for y in range(0, n)]
		board[0][0] = 0
		if result[move_y - 1][move_x - 1] == -2 and result[move_x - 1][move_y - 1] == -2:
			reccur_board(board, 0, 0, move_x, move_y)
			result[move_y - 1][move_x - 1] = str(board[n - 1][n - 1])
			result[move_x - 1][move_y - 1] = str(board[n - 1][n - 1])
for line in result:
	print(' '.join([str(e) for e in line]))


import collections as col
import sys

def valids(n, a, b, v):
	ws = []
	(x, y) = v
	if 0 <= x + a < n and 0 <= y + b < n:
		ws.append((x + a, y + b))
	if 0 <= x + a < n and 0 <= y - b < n:
		ws.append((x + a, y - b))
	if 0 <= x - a < n and 0 <= y + b < n:
		ws.append((x - a, y + b))
	if 0 <= x - a < n and 0 <= y - b < n:
		ws.append((x - a, y - b))
	if 0 <= x + b < n and 0 <= y + a < n:
		ws.append((x + b, y + a))
	if 0 <= x + b < n and 0 <= y - a < n:
		ws.append((x + b, y - a))
	if 0 <= x - b < n and 0 <= y + a < n:
		ws.append((x - b, y + a))
	if 0 <= x - b < n and 0 <= y - a < n:
		ws.append((x - b, y - a))
	return ws

def bfs(n, a, b):
	marked = [False for _ in range(n * n)]
	marked[0] = True
	dist = [0 for _ in range(n * n)]
	q = col.deque()
	q.append((0, 0))
	while len(q) > 0:
		v = q.popleft()
		vIndex = v[0] * n + v[1]
		for w in valids(n, a, b, v):
			wIndex = w[0] * n + w[1]
			if not marked[wIndex]:
				marked[wIndex] = True
				dist[wIndex] = dist[vIndex] + 1
				if w == (n - 1, n - 1):
					return dist[wIndex]
				q.append(w)
	return -1
n = int(input().strip())
res = [[-1 for _ in range(n)] for _ in range(n)]
for i in range(n - 1):
	for j in range(i, n - 1):
		res[i][j] = res[j][i] = bfs(n, i + 1, j + 1)
for i in range(n - 1):
	print(' '.join([str(res[i][j]) for j in range(n - 1)]))


import collections
import heapq
import sys
n = int(input().strip())
GOAL = (n - 1, n - 1)

def heuristic(square, a, b):
	return (GOAL[0] + GOAL[1] - square[0] - square[1]) / (a + b)

def is_legal(square):
	return 0 <= square[0] < n and 0 <= square[1] < n

def get_neighbors(square, a, b):
	diffs = ((a, b), (a, -b), (-a, b), (-a, -b), (b, a), (b, -a), (-b, a), (-b, -a))
	neighbors = set([])
	for d in diffs:
		neighbor = (square[0] + d[0], square[1] + d[1])
		if is_legal(neighbor):
			neighbors.add(neighbor)
	return neighbors

def moves_to_goal(a, b):
	closed_squares = set([])
	frontier_squares = [(0, [(0, 0), 0])]
	while len(frontier_squares):
		(current_square, path_len) = heapq.heappop(frontier_squares)[1]
		if current_square in closed_squares:
			continue
		closed_squares.add(current_square)
		neighbors = get_neighbors(current_square, a, b)
		for neighbor in neighbors:
			if neighbor == GOAL:
				return path_len + 1
			if not neighbor in closed_squares:
				heapq.heappush(frontier_squares, (path_len + 1 + heuristic(neighbor, a, b), [neighbor, path_len + 1]))
	return -1
for a in range(1, n):
	row = []
	for b in range(1, n):
		row.append(str(moves_to_goal(a, b)))
	print(' '.join(row))


import sys

def next_step(board, target, a, b, n):
	mask = 0
	for i in range(n):
		for j in range(n):
			if board[n + i][n + j] == target:
				mask = 1
				if board[n + i + a][n + j + b] == 0:
					board[n + i + a][n + j + b] = target + 1
				if board[n + i + a][n + j - b] == 0:
					board[n + i + a][n + j - b] = target + 1
				if board[n + i + b][n + j + a] == 0:
					board[n + i + b][n + j + a] = target + 1
				if board[n + i + b][n + j - a] == 0:
					board[n + i + b][n + j - a] = target + 1
				if board[n + i - a][n + j + b] == 0:
					board[n + i - a][n + j + b] = target + 1
				if board[n + i - a][n + j - b] == 0:
					board[n + i - a][n + j - b] = target + 1
				if board[n + i - b][n + j + a] == 0:
					board[n + i - b][n + j + a] = target + 1
				if board[n + i - b][n + j - a] == 0:
					board[n + i - b][n + j - a] = target + 1
	if mask == 0:
		return board
	return next_step(board, target + 1, a, b, n)

def check_step(a, b, n):
	board = []
	for i in range(3 * n):
		board.append([])
		for j in range(3 * n):
			board[i].append(0)
	board[n][n] = 1
	new_board = next_step(board, 1, a, b, n)
	return int(new_board[2 * n - 1][2 * n - 1] - 1)
n = int(input().strip())
for i in range(1, n):
	mylist = []
	for j in range(1, n):
		mylist.append(str(check_step(i, j, n)))
	print(' '.join(mylist))


import sys
from collections import deque

def main(n):
	for a in range(1, n):
		row = []
		for b in range(1, n):
			row.append(move_knight([a, b], n))
		print(' '.join((str(x) for x in row)))

def move_knight(movement, n):
	visited = [[0, 0]]
	positions = deque()
	positions.append([[0, 0], 0])
	moves = 0
	while positions:
		current = positions.popleft()
		c_position = current[0]
		c_move = current[1]
		future = future_positions(movement, current[0], n)
		for f in future:
			if f == [n - 1, n - 1]:
				return c_move + 1
			elif f in visited:
				pass
			else:
				positions.append([f, c_move + 1])
				visited.append(f)
	return -1

def future_positions(movement, current, n):
	f_ps = []
	x = current[0]
	y = current[1]
	a = movement[0]
	b = movement[1]
	for f_x in [x - a, x + a]:
		if valid(f_x, n):
			for f_y in [y - b, y + b]:
				if valid(f_y, n):
					f_ps.append([f_x, f_y])
	for f_x in [x - b, x + b]:
		if valid(f_x, n):
			for f_y in [y - a, y + a]:
				if valid(f_y, n):
					f_ps.append([f_x, f_y])
	return f_ps

def valid(i, n):
	return i >= 0 and i < n
n = int(input().strip())
main(n)


import sys

def new_positions(new_positions, c, d, length, pre_positions):
	x_change = [d, d, -d, -d, c, c, -c, -c]
	y_change = [c, -c, c, -c, d, -d, d, -d]
	positions = set([])
	for item in new_positions:
		positions = positions | set([(item[0] + x, item[1] + y) for (x, y) in zip(x_change, y_change) if 0 <= item[0] + x and item[0] + x < length and (0 <= item[1] + y) and (item[1] + y < length)])
	return positions - pre_positions
n = int(input().strip())
for i in range(1, n):
	str_row = ''
	for j in range(1, n):
		positions_all = set([])
		new_set = set([(n - 1, n - 1)])
		count = 0
		while len(new_set) > 0 and (0, 0) not in new_set:
			positions_all = positions_all | new_set
			new_set = new_positions(new_set, i, j, n, positions_all)
			count += 1
		str_row += str(count) + ' ' if (0, 0) in new_set else str(-1) + ' '
	print(str_row.strip())


class Game(object):

	def __init__(self, n):
		self.n = n
		self.knights = {(a, b) for a in range(1, n) for b in range(1, a + 1)}
		self.answers = {}
		self.initial = (0, 0)
		self.goal = (n - 1, n - 1)

	def available_moves(self, pos, k):
		possible_moves = [(pos[0] + x * k[i], pos[1] + y * k[int(not i)]) for x in (1, -1) for y in (1, -1) for i in (0, 1)]
		return {m for m in possible_moves if min(m) >= 0 and max(m) < self.n}

	def play(self, k):
		if k in self.answers:
			return self.answers[k]
		next_moves = {self.initial}
		played_moves = {}
		move_num = 0
		while len(next_moves) > 0:
			current_moves = next_moves
			next_moves = set()
			for m in current_moves:
				if m == self.goal:
					self.answers[k] = move_num
					self.answers[k[::-1]] = move_num
					return move_num
				played_moves[m] = move_num
				next_moves.update(self.available_moves(m, k))
			next_moves = next_moves.difference(played_moves.keys())
			move_num += 1
		self.answers[k] = -1
		self.answers[k[::-1]] = -1
		return -1

	def evaluate_all(self):
		for k in self.knights:
			x = self.play(k)
		output = [[0 for __ in range(self.n - 1)] for _ in range(self.n - 1)]
		for (k, v) in self.answers.items():
			output[k[0] - 1][k[1] - 1] = str(v)
		print('\n'.join([' '.join(_) for _ in output]))
n = int(input().strip())
game = Game(n)
game.evaluate_all()


import sys
import queue
n = int(input().strip())
(a, b) = (1, 1)

def valid_move(i, j):
	return 0 <= i < n and 0 <= j < n

def find_new_moves(i, j):
	moves = [(i + a, j + b), (i + a, j - b), (i - a, j + b), (i - a, j - b), (i + b, j + a), (i + b, j - a), (i - b, j + a), (i - b, j - a)]
	moves = [move for move in moves if valid_move(move[0], move[1])]
	return moves

def BFS():
	distance = [[-1] * n for i in range(n)]
	color = [['white'] * n for i in range(n)]
	parent = [[(-1, -1)] * n for i in range(n)]
	source = (0, 0)
	color[0][0] = 'gray'
	distance[0][0] = 0
	parent[0][0] = (-1, -1)
	q = queue.Queue(250)
	q.put(source)
	while not q.empty() and distance[n - 1][n - 1] is -1:
		u = q.get()
		for v in find_new_moves(u[0], u[1]):
			i = v[0]
			j = v[1]
			if color[i][j] is 'white':
				color[i][j] = 'gray'
				distance[i][j] = distance[u[0]][u[1]] + 1
				parent[i][j] = u
				q.put((i, j))
		color[u[0]][u[1]] = 'black'
	return distance[n - 1][n - 1]
for i in range(1, n):
	paths = []
	for j in range(1, n):
		(a, b) = (i, j)
		answer = BFS()
		paths.append(str(answer))
	print(' '.join(paths))


import sys
from queue import Queue

def in_range(i, j):
	return 0 <= i < n and 0 <= j < n
n = int(input().strip())
ans = [[-1 for i in range(n - 1)] for j in range(n - 1)]
for i in range(1, n):
	for j in range(1, n):
		board = [[-1 for i in range(n)] for j in range(n)]
		board[0][0] = 0
		q = Queue()
		visited = set()
		q.put((0, 0))
		visited.add((0, 0))
		while not q.empty():
			(x, y) = q.get()
			step = board[x][y]
			if (x, y) == (n - 1, n - 1):
				ans[i - 1][j - 1] = board[n - 1][n - 1]
				break
			set_of_moves = [(x + c1 * i, y + c2 * j) for c1 in [-1, 1] for c2 in [-1, 1]] + [(x + c1 * j, y + c2 * i) for c1 in [-1, 1] for c2 in [-1, 1]]
			for pt in set_of_moves:
				if in_range(*pt) and pt not in visited:
					board[pt[0]][pt[1]] = step + 1
					visited.add(pt)
					q.put(pt)
for ar in ans:
	print(*ar)


import sys
n = int(input().strip())
from queue import deque

def get_Neighbours(p, i, j):
	potential_neighbours = [(p[0] + i, p[1] + j), (p[0] + j, p[1] + i), (p[0] - i, p[1] + j), (p[0] - j, p[1] + i), (p[0] + i, p[1] - j), (p[0] + j, p[1] - i), (p[0] - i, p[1] - j), (p[0] - j, p[1] - i)]
	neighbours = []
	for item in potential_neighbours:
		if item[0] >= 0 and item[1] >= 0 and (item[0] < n) and (item[1] < n):
			neighbours.append(item)
	return neighbours
count = [[None for _ in range(n - 1)] for _ in range(n - 1)]
for i in range(1, n):
	for j in range(i, n):
		visited = [[False for _ in range(n)] for _ in range(n)]
		counter = [[float('inf') for _ in range(n)] for _ in range(n)]
		counter[0][0] = 0
		queue = deque([(0, 0)])
		while len(queue):
			cP = queue.pop()
			if visited[cP[0]][cP[1]]:
				continue
			visited[cP[0]][cP[1]] = True
			neighbours = get_Neighbours(cP, i, j)
			for item in neighbours:
				queue.appendleft(item)
				counter[item[0]][item[1]] = min(counter[item[0]][item[1]], counter[cP[0]][cP[1]] + 1)
		t = counter[n - 1][n - 1]
		if t == float('inf'):
			count[i - 1][j - 1] = count[j - 1][i - 1] = -1
		else:
			count[i - 1][j - 1] = count[j - 1][i - 1] = t
for i in range(n - 1):
	print(' '.join(list(map(str, count[i]))))


import queue

def solution(n, b):
	if b[0] >= n or b[0] < 0:
		return False
	if b[1] >= n or b[1] < 0:
		return False
	return True

def result(n, y1, a):
	res = []
	for y2 in range(1, n):
		if (y1, y2) in a:
			res.append(a[y1, y2])
		elif (y2, y1) in a:
			res.append(a[y2, y1])
		else:
			rep = search(y1, y2, n)
			a[y1, y2] = rep
			res.append(rep)
	return res

def search(y1, y2, n):
	x = {}
	y = {}
	worklist = queue.Queue()
	worklist.put((0, 0))
	while worklist.qsize() != 0:
		b = worklist.get()
		if b in x:
			continue
		x[b] = True
		for k in compute(y1, y2, n, b):
			if solution(n, k) and k not in x:
				worklist.put(k)
				y[k] = b
			else:
				continue
	if (n - 1, n - 1) in x:
		res = sol(n, y)
		return res
	else:
		return -1

def compute(y1, y2, n, b):
	x1 = b[0]
	x2 = b[1]
	arr = []
	arr.append((x1 + y1, x2 + y2))
	arr.append((x1 + y1, x2 - y2))
	arr.append((x1 - y1, x2 + y2))
	arr.append((x1 - y1, x2 - y2))
	arr.append((x1 + y2, x2 + y1))
	arr.append((x1 + y2, x2 - y1))
	arr.append((x1 - y2, x2 + y1))
	arr.append((x1 - y2, x2 - y1))
	return arr

def sol(n, y):
	tot = 0
	b = (n - 1, n - 1)
	while b in y:
		p = y[b]
		tot = tot + 1
		if p == (0, 0):
			return tot
		else:
			b = p
n = int(input())
a = {}
for i in range(1, n):
	res = result(n, i, a)
	print(str(res)[1:-1].replace(',', ''))


import sys
n = int(input().strip())
m = [[-1 for x in range(n)] for y in range(n)]
g = 1
while g <= n - 1:
	d = 1
	while d <= n - 1:
		visited = []
		visited.append((n - 1, n - 1))
		counter = 0
		cur_len = 0
		diff = len(visited) - cur_len
		if m[g][d] == -1:
			while len(visited) > cur_len and (0, 0) not in visited:
				cur_len = len(visited)
				arr = visited[len(visited) - diff:len(visited)]
				h = 0
				while h <= len(arr) - 1:
					(t, r) = arr[h]
					if (t - g <= n - 1 and t - g >= 0) and (r - d <= n - 1 and r - d >= 0) and ((t - g, r - d) not in visited):
						visited.append((t - g, r - d))
					if (t - g <= n - 1 and t - g >= 0) and (r + d <= n - 1 and r + d >= 0) and ((t - g, r + d) not in visited):
						visited.append((t - g, r + d))
					if (t + g <= n - 1 and t + g >= 0) and (r - d <= n - 1 and r - d >= 0) and ((t + g, r - d) not in visited):
						visited.append((t + g, r - d))
					if (t + g <= n - 1 and t + g >= 0) and (r + d <= n - 1 and r + d >= 0) and ((t + g, r + d) not in visited):
						visited.append((t + g, r + d))
					if (t - d <= n - 1 and t - d >= 0) and (r - g <= n - 1 and r - g >= 0) and ((t - d, r - g) not in visited):
						visited.append((t - d, r - g))
					if (t - d <= n - 1 and t - d >= 0) and (r + g <= n - 1 and r + g >= 0) and ((t - d, r + g) not in visited):
						visited.append((t - d, r + g))
					if (t + d <= n - 1 and t + d >= 0) and (r - g <= n - 1 and r - g >= 0) and ((t + d, r - g) not in visited):
						visited.append((t + d, r - g))
					if (t + d <= n - 1 and t + d >= 0) and (r + g <= n - 1 and r + g >= 0) and ((t + d, r + g) not in visited):
						visited.append((t + d, r + g))
					h = h + 1
				diff = len(visited) - cur_len
				counter = counter + 1
			if (0, 0) in visited:
				m[g][d] = counter
				m[d][g] = m[g][d]
		d = d + 1
	g = g + 1
p = 1
while p <= n - 1:
	line = ''
	g = 1
	while g <= n - 1:
		line = line + ' ' + str(m[p][g])
		g = g + 1
	print(line.lstrip())
	p = p + 1


import sys
n = int(input().strip())

class Graph:

	def __init__(self, board_size, a, b):
		self.board_size = board_size
		nodes = [(x, y) for x in range(board_size) for y in range(board_size)]
		self.path_dict = {k: set() for k in nodes}
		for node in self.path_dict:
			self.path_dict[node] = {(node[0] + x, node[1] + y) for (x, y) in [(a, b), (-a, -b), (a, -b), (-a, b), (b, a), (-b, -a), (b, -a), (-b, a)] if 0 <= node[0] + x < board_size and 0 <= node[1] + y < board_size}

	def solution(self):
		distance = {(x, y): -1 for x in range(self.board_size) for y in range(self.board_size)}
		visited = {(self.board_size - 1, self.board_size - 1)}
		currentDistance = 0
		toVisitNext = self.path_dict[self.board_size - 1, self.board_size - 1]
		distance[self.board_size - 1, self.board_size - 1] = 0
		while len(toVisitNext) > 0:
			newDestinations = set()
			currentDistance += 1
			for node in toVisitNext:
				visited.add(node)
				distance[node] = currentDistance
				newDestinations |= self.path_dict[node]
			toVisitNext = set(newDestinations) - visited
		return distance[0, 0]
for a in range(1, n):
	row = ''
	for b in range(1, n):
		graph = Graph(n, a, b)
		row += str(graph.solution()) + ' '
	print(row.strip())


import sys
n = int(input().strip())

def get_moves(p, d):
	moves = set()
	for i in (d[0], -d[0]):
		for j in (d[1], -d[1]):
			x = p[0] + i
			y = p[1] + j
			move = (x, y)
			if 0 <= x < n and 0 <= y < n:
				moves.add((x, y))
	return moves

def make_move(p, d):
	m1 = get_moves(p, d)
	m2 = get_moves(p, d[::-1])
	poss = set.union(m1, m2)
	return poss

def get_to(init, dest, step):
	history = set()
	c = 0
	history.add(init)
	obs = [(init, c)]
	while obs:
		(point, cost) = obs.pop(0)
		np = make_move(point, step)
		for p in np:
			if p == dest:
				return cost + 1
			if not p in history:
				history.add(p)
				obs.append((p, cost + 1))
	return -1

def build_matrix(n):
	init = (0, 0)
	dest = (n - 1, n - 1)
	res = [[None for i in range(n - 1)] for j in range(n - 1)]
	for i in range(1, n):
		for j in range(1, i + 1):
			step = (i, j)
			cost = get_to(init, dest, step)
			res[i - 1][j - 1] = cost
			res[j - 1][i - 1] = cost
	for x in res:
		line = ' '.join([str(y) for y in x])
		print(line)

def test(num):
	from timeit import default_timer as timer
	t = timer()
	for i in range(num):
		build_matrix(n)
	print(timer() - t)
build_matrix(n)


n = int(input().strip())
pairs = []
(start, end) = ((0, 0), (n - 1, n - 1))

def validPos(pos):
	return pos[0] >= 0 and pos[1] >= 0 and (pos[0] < n) and (pos[1] < n)

def moves(x, pair):
	moves = []
	(a, b) = (pair[0], pair[1])
	(x1, x2) = (x[0], x[1])
	moves.append((x1 + a, x2 + b))
	moves.append((x1 + a, x2 - b))
	moves.append((x1 - a, x2 + b))
	moves.append((x1 - a, x2 - b))
	moves.append((x1 + b, x2 + a))
	moves.append((x1 + b, x2 - a))
	moves.append((x1 - b, x2 + a))
	moves.append((x1 - b, x2 - a))
	return set(filter(validPos, moves))

def minPath(pair):
	old_moves = [start]
	new_moves = moves(start, pair)
	count = 1
	while len(new_moves) > 0 and end not in new_moves:
		count += 1
		for move in new_moves.copy():
			if move not in old_moves:
				old_moves.append(move)
				new_moves.remove(move)
				for m in moves(move, pair):
					if m not in old_moves:
						new_moves.add(m)
	if len(new_moves) == 0:
		return -1
	else:
		return count
for i in range(1, n):
	print(*[minPath((i, j)) for j in range(1, n)], sep=' ')


import sys

def valid_move_combine(start, dpos, n):
	end = (start[0] + dpos[0], start[1] + dpos[1])
	if end[0] >= 0 and end[0] < n and (end[1] >= 0) and (end[1] < n):
		return end
	else:
		return None

def KnightL(a, b, n):
	halfMoves = [(a, b), (-a, b), (a, -b), (-a, -b)]
	moves = halfMoves + [(y, x) for (x, y) in halfMoves]
	moves = set(moves)
	Q = []
	start = (0, 0)
	end = (n - 1, n - 1)
	Q.append([start])
	visited = set()
	while Q:
		path = Q.pop(0)
		node = path[-1]
		if node == end:
			return path
		if node not in visited:
			visited.add(node)
			for dpos in moves:
				adj = valid_move_combine(node, dpos, n)
				if adj:
					Q.append(path + [adj])
	return None
n = int(input().strip())
for a in range(1, n):
	res = []
	for b in range(1, n):
		temp = KnightL(a, b, n)
		if temp:
			res.append(len(temp) - 1)
		else:
			res.append(-1)
	print(*res, sep=' ')


import sys
from queue import Queue

def next_valid_cells(visited, x, y, a_delta, b_delta, n):
	valid_cells = []
	up = x - a_delta
	down = x + a_delta
	left = y - b_delta
	right = y + b_delta
	if (up >= 0 and left >= 0) and (up, left) not in visited:
		valid_cells.append((up, left))
		visited.add((up, left))
	if (up >= 0 and right < n) and (up, right) not in visited:
		valid_cells.append((up, right))
		visited.add((up, right))
	if (down < n and left >= 0) and (down, left) not in visited:
		valid_cells.append((down, left))
		visited.add((down, left))
	if (down < n and right < n) and (down, right) not in visited:
		valid_cells.append((down, right))
		visited.add((down, right))
	return valid_cells

def breadth_first_search(a_delta, b_delta, n):
	visited = set()
	q = Queue()
	q.put([(0, 0), 0])
	while q.empty() == False:
		((curr_x, curr_y), depth) = q.get()
		if curr_x == n - 1 and curr_y == n - 1:
			return depth
		next_cells = next_valid_cells(visited, curr_x, curr_y, a_delta, b_delta, n)
		next_cells += next_valid_cells(visited, curr_x, curr_y, b_delta, a_delta, n)
		for cell in next_cells:
			if cell == (n - 1, n - 1):
				return depth + 1
			q.put([cell, depth + 1])
	return -1

def main():
	n = int(input().strip())
	final_result = [['-1'] * (n - 1) for _ in range(n - 1)]
	for outer in range(1, n):
		for inner in range(outer, n):
			if inner == 1 and outer == 1:
				final_result[0][0] = str(n - 1)
			elif inner == n - 1 and outer == n - 1:
				final_result[inner - 1][outer - 1] = '1'
			else:
				r = str(breadth_first_search(outer, inner, n))
				final_result[inner - 1][outer - 1] = r
				final_result[outer - 1][inner - 1] = r
	for row in final_result:
		print(' '.join(row))
main()


import sys
n = int(input().strip())

def find_reachable_squares(x, y, a, b, n):
	squares = set()
	if x + a >= 0 and x + a < n:
		if y + b >= 0 and y + b < n:
			squares.add((x + a, y + b))
		if y - b >= 0 and y - b < n:
			squares.add((x + a, y - b))
	if x - a >= 0 and x - a < n:
		if y + b >= 0 and y + b < n:
			squares.add((x - a, y + b))
		if y - b >= 0 and y - b < n:
			squares.add((x - a, y - b))
	if x + b >= 0 and x + b < n:
		if y + a >= 0 and y + a < n:
			squares.add((x + b, y + a))
		if y - a >= 0 and y - a < n:
			squares.add((x + b, y - a))
	if x - b >= 0 and x - b < n:
		if y + a >= 0 and y + a < n:
			squares.add((x - b, y + a))
		if y - a >= 0 and y - a < n:
			squares.add((x - b, y - a))
	return squares
matrix = [[0 for x in range(n - 1)] for y in range(n - 1)]
for i in range(1, n):
	for j in range(i, n):
		reachable_squares = set([(n - 1, n - 1)])
		previous_squares = set([(n - 1, n - 1)])
		history_sq = set([(n - 1, n - 1)])
		count = 0
		while len(reachable_squares) > 0 and (0, 0) not in history_sq:
			reachable_squares = set()
			count = count + 1
			for s in previous_squares:
				reachable_squares = reachable_squares | find_reachable_squares(s[0], s[1], i, j, n)
			reachable_squares = reachable_squares - history_sq
			previous_squares = set()
			for s in reachable_squares:
				previous_squares.add(s)
				history_sq.add(s)
		val = -1
		if (0, 0) in history_sq:
			val = count
		matrix[i - 1][j - 1] = val
		matrix[j - 1][i - 1] = val
for i in matrix:
	string = ''
	for j in i:
		string = string + ' ' + str(j)
	print(string[1:])


import sys

def make_act_set(a, b):
	actset = []
	actset.append([a, b])
	actset.append([-a, b])
	actset.append([a, -b])
	actset.append([-a, -b])
	actset.append([b, a])
	actset.append([-b, a])
	actset.append([b, -a])
	actset.append([-b, -a])
	return actset

def make_xy_set(N):
	xyset = []
	for x in range(N):
		for y in range(N):
			xyset.append([x, y])
	xyset.remove([0, 0])
	return xyset

def take_step(cur_xy_list, actset, xyset, N):
	new_xy_list = []
	for curxy in cur_xy_list:
		for act in actset:
			new_xy = [i for i in curxy]
			new_xy[0] += act[0]
			new_xy[1] += act[1]
			if new_xy in xyset:
				new_xy_list.append(new_xy)
				xyset.remove(new_xy)
		if not [N - 1, N - 1] in xyset:
			break
	return (new_xy_list, xyset)

def find_sol(N, index_pair):
	xyset = make_xy_set(N)
	actset = make_act_set(index_pair[0], index_pair[1])
	step = 0
	cur_xy_list = [[0, 0]]
	while 1:
		(cur_xy_list, xyset) = take_step(cur_xy_list, actset, xyset, N)
		step += 1
		if len(cur_xy_list) == 0:
			step = -1
			break
		elif not [N - 1, N - 1] in xyset:
			break
	return step
N = int(input().strip())
sol = []
for loof in range(N - 1):
	sol.append([0 for i in range(N - 1)])
for row in range(N - 1):
	for col in range(N - 1):
		if row > col:
			sol[row][col] = sol[col][row]
		else:
			sol[row][col] = find_sol(N, [row + 1, col + 1])
for i in range(N - 1):
	for j in range(N - 1):
		if j < N - 2:
			print(sol[i][j], end=' ')
		else:
			print(sol[i][j])


import sys
debug = False
inf = 1e+100

def minmove(n, a, b):
	if debug:
		print('init', n, a, b)
	memo = [[inf] * n for i in range(n)]

	def move(x, y, step):
		if x < 0 or x > n - 1:
			return False
		if y < 0 or y > n - 1:
			return False
		if memo[x][y] <= step:
			return False
		memo[x][y] = step
		if x == n - 1 and y == n - 1:
			if debug:
				print('memo', x, y, step)
			return True
		step += 1
		move(x + a, y + b, step)
		move(x + a, y - b, step)
		move(x - a, y + b, step)
		move(x - a, y - b, step)
		move(x + b, y + a, step)
		move(x + b, y - a, step)
		move(x - b, y + a, step)
		move(x - b, y - a, step)
	move(0, 0, 0)
	if memo[n - 1][n - 1] < inf:
		return memo[n - 1][n - 1]
	else:
		return -1
n = int(input().strip())
minmoves = [[0] * (n - 1) for i in range(n - 1)]
for a in range(1, n):
	for b in range(1, a + 1):
		i = a - 1
		j = b - 1
		minmoves[i][j] = minmove(n, a, b)
		minmoves[j][i] = minmoves[i][j]
		if debug:
			print('save', i, j, minmoves[i][j])
for i in range(n - 1):
	print(' '.join([str(s) for s in minmoves[i]]))


import sys
import queue
memo = dict()
n = int(input().strip())
memo[1, 1] = n - 1
for i in range(1, n):
	for j in range(1, n):
		if (min(i, j), max(i, j)) in memo:
			continue
		q = queue.Queue()
		q.put(((0, 0), 0, []))
		done = set()
		while True:
			if q.empty():
				memo[min(i, j), max(i, j)] = -1
				break
			(pos, nmoves, prevmoves) = q.get()
			if pos in done:
				continue
			done.add(pos)

			def move():
				(x, y) = pos
				for mx in (-1, 1):
					for my in (-1, 1):
						for r in (True, False):
							mv = (x + i * mx, y + j * my) if r else (x + j * mx, y + i * my)
							if mv == (n - 1, n - 1):
								memo[min(i, j), max(i, j)] = nmoves + 1
								return
							if mv[0] >= 0 and mv[1] >= 0 and (mv[0] < n) and (mv[1] < n) and (mv not in done):
								q.put((mv, nmoves + 1, prevmoves + [mv]))
			move()
			if (min(i, j), max(i, j)) in memo:
				break
	print(' '.join([str(memo[min(i, j), max(i, j)]) for j in range(1, n)]))


import sys

def min_dist(n, a, b):
	start = (0, 0)
	finish = (n - 1, n - 1)
	chessboard = dict()
	chessboard[start] = 0
	frontline = [start]
	while len(frontline):
		(row, col) = frontline.pop(0)
		next = [(row + r, col + c) for r in [-a, a] for c in [-b, b]]
		next.extend([(row + r, col + c) for r in [-b, b] for c in [-a, a]])
		next = filter(lambda pt: all((coord >= 0 and coord < n for coord in pt)), next)
		curr_step = chessboard[row, col] + 1
		for pt in next:
			if pt not in chessboard:
				chessboard[pt] = curr_step
				frontline.append(pt)
			elif chessboard[pt] > curr_step:
				chessboard[pt] = curr_step
				frontline.append(pt)
	return chessboard[finish] if finish in chessboard else -1
n = int(input().strip())
answer = [[min_dist(n, a, b) for a in range(1, n)] for b in range(1, n)]
for line in answer:
	print(*line)


import sys

def inBoardAndNotVisited(x, y, knight_visited, n):
	result = False
	if x >= 0 and y >= 0 and (x < n) and (y < n) and (not knight_visited[x][y]):
		result = True
	return result

def knightNewReached(x_ini, y_ini, x_goal, y_goal, jump, knight_visited, next_pos, knight_new_reach, n):
	for i in range(8):
		x_n = x_ini + next_pos[i][0]
		y_n = y_ini + next_pos[i][1]
		if inBoardAndNotVisited(x_n, y_n, knight_visited, n):
			knight_visited[x_n][y_n] = True
			knight_new_reach.add((x_n, y_n))

def knightl(a, b, x_goal, y_goal, n):
	jump = 0
	next_pos = [[a, b], [a, -b], [-a, b], [-a, -b], [b, a], [b, -a], [-b, a], [-b, -a]]
	x_ini = 0
	y_ini = 0
	knight_visited = [[False for j in range(n)] for i in range(n)]
	knight_visited[x_ini][y_ini] = True
	knight_reach = {(x_ini, y_ini)}
	reached = False
	while not reached and len(knight_reach) > 0:
		knight_new_reach = set()
		for i in knight_reach:
			x_ini = i[0]
			y_ini = i[1]
			knightNewReached(x_ini, y_ini, x_goal, y_goal, jump, knight_visited, next_pos, knight_new_reach, n)
		knight_reach = knight_new_reach
		jump += 1
		if (x_goal, y_goal) in knight_reach:
			reached = True
	if not reached:
		jump = -1
	return jump
n = int(input().strip())
paths = []
knight_taken = [[False for j in range(n)] for i in range(n)]
knight_value = [[0 for j in range(n)] for i in range(n)]
for a in range(1, n, 1):
	line = []
	for b in range(1, n, 1):
		if not knight_taken[a][b]:
			jump = knightl(a, b, n - 1, n - 1, n)
			knight_taken[a][b] = True
			knight_taken[b][a] = True
			knight_value[a][b] = jump
			knight_value[b][a] = jump
		line.append(knight_value[a][b])
	paths.append(line)
for line in paths:
	print(line[0], end='')
	for i in range(1, len(line), 1):
		print('', line[i], end='')
	print('')


def way_finder(a, b, register, position):
	global count
	newpositions = [position]
	count = 0
	while 1:
		result = 0
		count += 1
		for j in range(len(newpositions)):
			i = 0
			if newpositions[i][0] - b >= 0 and newpositions[i][1] - a >= 0:
				newposition = [newpositions[i][0] - b, newpositions[i][1] - a]
				if newposition == [0, 0]:
					result = 2
					break
				if newposition not in register and newposition not in newpositions:
					result = 1
					newpositions.append([newpositions[i][0] - b, newpositions[i][1] - a])
			if newpositions[i][0] - a >= 0 and newpositions[i][1] - b >= 0:
				newposition = [newpositions[i][0] - a, newpositions[i][1] - b]
				if newposition == [0, 0]:
					result = 2
					break
				if newposition not in register and newposition not in newpositions:
					result = 1
					newpositions.append([newpositions[i][0] - a, newpositions[i][1] - b])
			if newpositions[i][0] - a >= 0 and newpositions[i][1] + b < n:
				newposition = [newpositions[i][0] - a, newpositions[i][1] + b]
				if newposition == [0, 0]:
					result = 2
					break
				if newposition not in register and newposition not in newpositions:
					result = 1
					newpositions.append([newpositions[i][0] - a, newpositions[i][1] + b])
			if newpositions[i][0] + a < n and newpositions[i][1] - b >= 0:
				newposition = [newpositions[i][0] + a, newpositions[i][1] - b]
				if newposition == [0, 0]:
					result = 2
					break
				if newposition not in register and newposition not in newpositions:
					result = 1
					newpositions.append([newpositions[i][0] + a, newpositions[i][1] - b])
			if newpositions[i][0] - b >= 0 and newpositions[i][1] + a < n:
				newposition = [newpositions[i][0] - b, newpositions[i][1] + a]
				if newposition == [0, 0]:
					result = 2
					break
				if newposition not in register and newposition not in newpositions:
					result = 1
					newpositions.append([newpositions[i][0] - b, newpositions[i][1] + a])
			if newpositions[i][0] + b < n and newpositions[i][1] - a >= 0:
				newposition = [newpositions[i][0] + b, newpositions[i][1] - a]
				if newposition == [0, 0]:
					result = 2
					break
				if newposition not in register and newposition not in newpositions:
					result = 1
					newpositions.append([newpositions[i][0] + b, newpositions[i][1] - a])
			if newpositions[i][0] + a < n and newpositions[i][1] + b < n:
				newposition = [newpositions[i][0] + a, newpositions[i][1] + b]
				if newposition == [0, 0]:
					result = 2
					break
				if newposition not in register and newposition not in newpositions:
					result = 1
					newpositions.append([newpositions[i][0] + a, newpositions[i][1] + b])
			if newpositions[i][0] + b < n and newpositions[i][1] + a < n:
				newposition = [newpositions[i][0] + b, newpositions[i][1] + a]
				if newposition == [0, 0]:
					result = 2
					break
				if newposition not in register and newposition not in newpositions:
					result = 1
					newpositions.append([newpositions[i][0] + b, newpositions[i][1] + a])
			newpositions.pop(0)
		register = register + newpositions
		if result == 0:
			return -1
		if result == 2:
			return count
n = int(input())
resultmatrix = [[[] for x in range(n - 1)] for x in range(n - 1)]
for i in range(1, n):
	for j in range(1, n):
		count = 0
		position = [n - 1, n - 1]
		resultmatrix[i - 1][j - 1] = way_finder(i, j, [[n - 1, n - 1]], position)
for i in range(0, n - 1):
	print(' '.join(map(str, resultmatrix[i])))


import collections
n = int(input().strip())
GOAL = (n - 1, n - 1)

def get_neighbors(square, a, b):
	diffs = set([(a, b), (a, -b), (-a, b), (-a, -b), (b, a), (b, -a), (-b, a), (-b, -a)])
	for d in diffs:
		neighbor = (square[0] + d[0], square[1] + d[1])
		if 0 <= neighbor[0] < n and 0 <= neighbor[1] < n:
			yield neighbor

def moves_to_goal(a, b):
	closed_squares = set([(0, 0)])
	frontier_squares = collections.deque([((0, 0), 0)])
	while len(frontier_squares):
		(current_square, path_len) = frontier_squares.popleft()
		neighbors = get_neighbors(current_square, a, b)
		for neighbor in neighbors:
			if neighbor == GOAL:
				return path_len + 1
			if neighbor not in closed_squares:
				closed_squares.add(neighbor)
				frontier_squares.append((neighbor, path_len + 1))
	return -1
for a in range(1, n):
	row = []
	for b in range(1, n):
		row.append(str(moves_to_goal(a, b)))
	print(' '.join(row))


import sys
n = int(input().strip())
m = [[-1 for x in range(n)] for y in range(n)]
g = 1
while g <= n - 1:
	d = 1
	while d <= n - 1:
		visited = []
		visited.append((n - 1, n - 1))
		counter = 0
		cur_len = 0
		diff = len(visited) - cur_len
		if m[g][d] == -1:
			while len(visited) > cur_len and (0, 0) not in visited:
				cur_len = len(visited)
				arr = visited[len(visited) - diff:len(visited)]
				h = 0
				while h <= len(arr) - 1:
					(t, r) = arr[h]
					if (t - g <= n - 1 and t - g >= 0) and (r - d <= n - 1 and r - d >= 0) and ((t - g, r - d) not in visited):
						visited.append((t - g, r - d))
					if (t - g <= n - 1 and t - g >= 0) and (r + d <= n - 1 and r + d >= 0) and ((t - g, r + d) not in visited):
						visited.append((t - g, r + d))
					if (t + g <= n - 1 and t + g >= 0) and (r - d <= n - 1 and r - d >= 0) and ((t + g, r - d) not in visited):
						visited.append((t + g, r - d))
					if (t + g <= n - 1 and t + g >= 0) and (r + d <= n - 1 and r + d >= 0) and ((t + g, r + d) not in visited):
						visited.append((t + g, r + d))
					if (t - d <= n - 1 and t - d >= 0) and (r - g <= n - 1 and r - g >= 0) and ((t - d, r - g) not in visited):
						visited.append((t - d, r - g))
					if (t - d <= n - 1 and t - d >= 0) and (r + g <= n - 1 and r + g >= 0) and ((t - d, r + g) not in visited):
						visited.append((t - d, r + g))
					if (t + d <= n - 1 and t + d >= 0) and (r - g <= n - 1 and r - g >= 0) and ((t + d, r - g) not in visited):
						visited.append((t + d, r - g))
					if (t + d <= n - 1 and t + d >= 0) and (r + g <= n - 1 and r + g >= 0) and ((t + d, r + g) not in visited):
						visited.append((t + d, r + g))
					h = h + 1
				diff = len(visited) - cur_len
				counter = counter + 1
			if (0, 0) in visited:
				m[g][d] = counter
				m[d][g] = m[g][d]
		d = d + 1
	g = g + 1
p = 1
while p <= n - 1:
	line = ''
	g = 1
	while g <= n - 1:
		line = line + ' ' + str(m[p][g])
		g = g + 1
	print(line.lstrip())
	p = p + 1


from collections import deque
n = int(input())

def is_square(a):
	return a[0] >= 0 and a[1] >= 0 and (a[0] < n) and (a[1] < n)

def getDist(dx, dy):
	dist = {}
	que = deque()
	que.append((0, 0, 0))
	direc = [(dx, dy), (dy, dx), (-dx, dy), (-dy, dx), (-dx, -dy), (-dy, -dx), (dx, -dy), (dy, -dx)]
	while len(que) > 0:
		(a0, a1, d) = que.popleft()
		a = (a0, a1)
		if not is_square(a) or (a in dist and d >= dist[a]):
			continue
		dist[a] = d
		for dire in direc:
			que.append((a[0] + dire[0], a[1] + dire[1], d + 1))
	if (n - 1, n - 1) in dist:
		return dist[n - 1, n - 1]
	else:
		return -1
for dx in range(1, n):
	out = []
	for dy in range(1, n):
		out.append(getDist(dx, dy))
	print(*out)


from collections import deque
from itertools import product, permutations

def deltas(y, x):
	perms = permutations([y, x])
	mul = product((1, -1), repeat=2)
	return ((p[0] * m[0], p[1] * m[1]) for (p, m) in product(perms, mul))
n = int(input())
res = [[None] * (n - 1) for _ in range(n - 1)]
for i in range(1, n):
	for j in range(i, n):
		dp = [[-1] * n for _ in range(n)]
		que = deque(((0, 0),))
		dp[0][0] = 0
		while que:
			(y, x) = que.popleft()
			for (dy, dx) in deltas(i, j):
				y1 = y + dy
				x1 = x + dx
				if 0 <= y1 < n and 0 <= x1 < n and (dp[y1][x1] == -1):
					dp[y1][x1] = dp[y][x] + 1
					que.append((y1, x1))
		res[i - 1][j - 1] = dp[-1][-1]
		res[j - 1][i - 1] = dp[-1][-1]
print(*(' '.join((str(x) for x in row)) for row in res), sep='\n')


import sys
from collections import deque
import ast

def build_graph(graph, start, end, movement, n):
	queue = deque([start])
	visited = {start: False}
	count = 0
	while queue:
		u = queue[0]
		queue.remove(u)
		if not visited.get(u, False):
			count += 1
			visited[u] = True
			fill_neighbours(graph, u, movement, n)
			neighbours = graph.get(u)
			for v in neighbours:
				if not visited.get(v, False):
					queue.append(v)

def add_neighbour(graph, node, neighbours):
	for n in neighbours:
		if n not in graph.get(node, []):
			graph.get(node).append(neighbours)

def fill_neighbours(graph, node, movement, n):
	a = movement[0]
	b = movement[1]
	pos = ast.literal_eval(node)
	x = pos[0]
	y = pos[1]
	legal_pos = deque()
	for i in [x + a, x - a]:
		for j in [y - b, y + b]:
			if 0 <= i < n and 0 <= j < n:
				legal_pos.append((i, j))
	for i in [x + b, x - b]:
		for j in [y - a, y + a]:
			if 0 <= i < n and 0 <= j < n:
				legal_pos.append((i, j))
	for pos in legal_pos:
		if str(pos) in graph:
			neighbours = graph.get(str(pos))
		else:
			graph[str(pos)] = []
		add_neighbour(graph, node, str(pos))

def BFS(graph, source_id, target_id):
	Q = deque([source_id])
	distance = {source_id: 0}
	previous = {}
	Flag = {source_id: True}
	while Q:
		u = Q[0]
		Q.remove(u)
		for v in graph.get(u):
			if Flag.get(v) is not True:
				distance[v] = distance[u] + 1
				previous[v] = u
				Flag[v] = True
				Q.append(v)
	return (previous, distance)

def main():
	mode = int(input().strip())
	for i in range(1, mode):
		line = []
		for j in range(1, mode):
			start = str((0, 0))
			end = str((mode - 1, mode - 1))
			graph = {start: [], end: []}
			build_graph(graph, start, end, (i, j), mode)
			(previous, distance) = BFS(graph, start, end)
			d = distance.get(end, -1)
			line.append(d)
		print(*line)
main()


from collections import deque

def next_move(i, j, a, b, n):
	move1 = [(i + d1, j + d2) for d1 in (-a, a) for d2 in (-b, b)]
	move2 = [(i + d1, j + d2) for d1 in (-b, b) for d2 in (-a, a)]
	return filter(lambda t: all(map(lambda x: 0 <= x < n, t)), move1 + move2)

def knightl(a, b, n):
	visited = [[False for _ in range(n)] for _ in range(n)]
	visited[0][0] = True
	q = deque([(0, 0, 0)])
	while q:
		(old_x, old_y, c) = q.popleft()
		for newpos in next_move(old_x, old_y, a, b, n):
			(x, y) = newpos
			if not visited[x][y]:
				if (x, y) == (n - 1, n - 1):
					return c + 1
				visited[x][y] = True
				q.append((x, y, c + 1))
	return -1
n = int(input().strip())
for a in range(1, n):
	line = []
	for b in range(1, n):
		line.append(knightl(a, b, n))
	print(*line)


import collections
n = int(input())
result = [[-1] * (n - 1) for i in range(n - 1)]
for a in range(1, n + 1):
	for b in range(1, n + 1):
		moves = [(-a, -b), (-a, b), (a, -b), (a, b), (-b, -a), (-b, a), (b, -a), (b, a)]
		visited = [[False] * n for i in range(n)]
		q = collections.deque([((0, 0), 0)])
		visited[0][0] = True
		while q:
			(x, steps) = q.popleft()
			visited[x[0]][x[1]] = True
			if x == (n - 1, n - 1):
				result[a - 1][b - 1] = steps
				break
			for move in moves:
				target = (x[0] + move[0], x[1] + move[1])
				if 0 <= target[0] < n and 0 <= target[1] < n and (not visited[target[0]][target[1]]):
					q.append((target, steps + 1))
					visited[target[0]][target[1]] = True
for row in result:
	print(*row)


import sys
from collections import deque

def check_v(n, sol, c, x, y):
	if x >= 0 and x < n and (y >= 0) and (y < n):
		if sol[x][y] == -1:
			sol[x][y] = c + 1
			return [c + 1, x, y]

def dfs(n, a, b):
	sol = [[-1 for _ in range(n)] for _ in range(n)]
	sol[0][0] = 0
	q = deque()
	q.append([0, 0, 0])
	while len(q) > 0:
		(c, x, y) = q.popleft()
		nodes = list()
		nodes.append(check_v(n, sol, c, x + a, y + b))
		nodes.append(check_v(n, sol, c, x + a, y - b))
		nodes.append(check_v(n, sol, c, x - a, y + b))
		nodes.append(check_v(n, sol, c, x - a, y - b))
		nodes.append(check_v(n, sol, c, x + b, y + a))
		nodes.append(check_v(n, sol, c, x + b, y - a))
		nodes.append(check_v(n, sol, c, x - b, y + a))
		nodes.append(check_v(n, sol, c, x - b, y - a))
		for node in nodes:
			if node != None:
				q.append(node)
	return sol[n - 1][n - 1]
n = int(input().strip())
sol = list()
for i in range(1, n):
	l = list()
	sol.append(l)
	for j in range(1, n):
		l.append(dfs(n, i, j))
for l in sol:
	print(' '.join([str(i) for i in l]))


import sys
n = int(input().strip())
for i in range(1, n):
	mvs = []
	for j in range(1, n):
		v = set([])
		q = [(0, 0)]
		ql = [0]
		while q:
			x = q.pop(0)
			lv = ql.pop(0)
			if x == (n - 1, n - 1):
				mvs.append(lv)
				break
			if x in v:
				continue
			v.add(x)
			l = [y for y in [(x[0] + j, x[1] + i), (x[0] + i, x[1] + j), (x[0] + j, x[1] - i), (x[0] + i, x[1] - j), (x[0] - j, x[1] - i), (x[0] - i, x[1] - j), (x[0] - j, x[1] + i), (x[0] - i, x[1] + j)] if y[0] >= 0 and y[0] < n and (y[1] >= 0) and (y[1] < n)]
			q.extend(l)
			ql.extend([lv + 1 for _ in l])
		if not q:
			mvs.append(-1)
	print(' '.join([str(x) for x in mvs]))


import sys

def all_moves(n, p, l):
	(a, b) = l
	(x, y) = p
	if x - a >= 0 and y - b >= 0:
		yield (x - a, y - b)
	if x - a >= 0 and y + b <= n:
		yield (x - a, y + b)
	if y - a >= 0 and x - b >= 0:
		yield (x - b, y - a)
	if y - a >= 0 and x + b <= n:
		yield (x + b, y - a)
	if x + a <= n and y - b >= 0:
		yield (x + a, y - b)
	if x + a <= n and y + b <= n:
		yield (x + a, y + b)
	if y + a <= n and x - b >= 0:
		yield (x - b, y + a)
	if y + a <= n and x + b <= n:
		yield (x + b, y + a)

def play(N, l):
	G = {(0, 0): {'visited': False, 'distance': 0, 'prev': None}, (N, N): {'visited': False, 'distance': float('inf'), 'prev': None}}
	p = (0, 0)
	while True:
		c = G[p]
		for neighbor in all_moves(N, p, l):
			if neighbor not in G:
				G[neighbor] = {'visited': False, 'distance': float('inf'), 'prev': None}
			n = G[neighbor]
			if not n['visited']:
				new_distance = c['distance'] + 1
				if new_distance < n['distance']:
					n['distance'] = new_distance
					n['prev'] = p
		c['visited'] = True
		if p == (N, N):
			break
		p = sorted(filter(lambda l: not l[1]['visited'], G.items()), key=lambda l: l[1]['distance'])[0][0]
		if G[p] == float('inf'):
			return -1
	p = (N, N)
	if G[p]['prev'] is None:
		return -1
	sum = 0
	while True:
		sum += 1
		p = G[p]['prev']
		if p == (0, 0):
			break
	return sum
n = int(input().strip())
for a in range(1, n):
	ans = []
	for b in range(1, n):
		ans.append(play(n - 1, (a, b)))
	print(' '.join(map(str, ans)))


from queue import Queue

def getNumMovesForKnight(i, j, n):
	s = set()
	q = Queue()
	root = (0, 0, 0)
	s.add(root)
	q.put(root)
	moves = [(i, j), (-i, j), (i, -j), (-i, -j), (j, i), (-j, i), (j, -i), (-j, -i)]
	while not q.empty():
		c = q.get()
		if c[0] == n - 1 and c[1] == n - 1:
			return str(c[2])
		for move in moves:
			x = c[0] + move[0]
			y = c[1] + move[1]
			if x >= 0 and x < n and (y >= 0) and (y < n) and ((x, y) not in s):
				s.add((x, y))
				q.put((x, y, c[2] + 1))
	return '-1'
n = int(input().strip())
for i in range(1, n):
	numMoves = []
	for j in range(1, n):
		numMoves.append(getNumMovesForKnight(i, j, n))
	print(' '.join(numMoves))


from collections import deque

def dfs(i, j):
	board = tuple(([-1] * n for _ in range(n)))
	board[0][0] = 0
	cache = deque(((0, 0),))
	while cache:
		(x, y) = cache.popleft()
		steps = board[x][y] + 1
		for (a, b) in ((x + i, y + j), (x + i, y - j), (x - i, y + j), (x - i, y - j), (x + j, y + i), (x + j, y - i), (x - j, y + i), (x - j, y - i)):
			if 0 <= a < n and 0 <= b < n and (board[a][b] == -1):
				cache.append((a, b))
				board[a][b] = steps
	return str(board[-1][-1])
n = int(input().strip())
nm1 = n - 1
result = tuple(([None] * nm1 for _ in range(nm1)))
for i in range(nm1):
	for j in range(i, nm1):
		result[i][j] = result[j][i] = dfs(i + 1, j + 1)
print('\n'.join((' '.join(row) for row in result)))


import sys

def check(x, y, n, b, p):
	if x >= 0 and x < n and (y >= 0) and (y < n) and (b[x][y] == -1 or b[x][y] > p):
		return True
	return False
n = int(input().strip())
res = [[-1] * n for k in range(n)]
for i in range(1, n, 1):
	res = []
	for j in range(1, n, 1):
		q = [[0, 0, 0]]
		b = [[-1] * n for k in range(n)]
		b[0][0] = 0
		while q:
			new_q = []
			for k in range(len(q)):
				p = q[k]
				if check(p[0] + i, p[1] + j, n, b, p[2] + 1) == True:
					b[p[0] + i][p[1] + j] = p[2] + 1
					new_q.append([p[0] + i, p[1] + j, p[2] + 1])
				if check(p[0] + j, p[1] + i, n, b, p[2] + 1) == True:
					b[p[0] + j][p[1] + i] = p[2] + 1
					new_q.append([p[0] + j, p[1] + i, p[2] + 1])
				if check(p[0] - i, p[1] + j, n, b, p[2] + 1) == True:
					b[p[0] - i][p[1] + j] = p[2] + 1
					new_q.append([p[0] - i, p[1] + j, p[2] + 1])
				if check(p[0] - j, p[1] + i, n, b, p[2] + 1) == True:
					b[p[0] - j][p[1] + i] = p[2] + 1
					new_q.append([p[0] - j, p[1] + i, p[2] + 1])
				if check(p[0] - i, p[1] - j, n, b, p[2] + 1) == True:
					b[p[0] - i][p[1] - j] = p[2] + 1
					new_q.append([p[0] - i, p[1] - j, p[2] + 1])
				if check(p[0] - j, p[1] - i, n, b, p[2] + 1) == True:
					b[p[0] - j][p[1] - i] = p[2] + 1
					new_q.append([p[0] - j, p[1] - i, p[2] + 1])
				if check(p[0] + i, p[1] - j, n, b, p[2] + 1) == True:
					b[p[0] + i][p[1] - j] = p[2] + 1
					new_q.append([p[0] + i, p[1] - j, p[2] + 1])
				if check(p[0] + j, p[1] - i, n, b, p[2] + 1) == True:
					b[p[0] + j][p[1] - i] = p[2] + 1
					new_q.append([p[0] + j, p[1] - i, p[2] + 1])
			q = new_q
		res.append(b[n - 1][n - 1])
	print(' '.join(('{0}'.format(r) for r in res)))


import sys

def compute(N, a, b):
	pMap = {(0, 0): 0}
	newPositions = [(0, 0)]
	target = (N - 1, N - 1)
	while len(newPositions):
		(px, py) = newPositions.pop(0)
		dests = [(px + a, py + b), (px + a, py - b), (px - a, py + b), (px - a, py - b), (px + b, py + a), (px + b, py - a), (px - b, py + a), (px - b, py - a)]
		for d in dests:
			if d not in pMap and 0 <= d[0] < N and (0 <= d[1] < N):
				moves = pMap[px, py] + 1
				if d == target:
					return moves
				pMap[d] = moves
				newPositions.append(d)
	return -1
N = int(input().strip())
for i in range(1, N):
	rowVals = []
	for j in range(1, N):
		rowVals.append(compute(N, i, j))
	print(' '.join((str(x) for x in rowVals)))


import sys

def get_frontier(cur, level, i, j, n):
	frontier = []
	for k in [-i, i]:
		for m in [-j, j]:
			for (p1, p2) in [(cur[0] + k, cur[1] + m), (cur[0] + m, cur[1] + k)]:
				if 0 <= p1 <= n - 1 and 0 <= p2 <= n - 1 and ((p1, p2) not in level):
					frontier.append((p1, p2))
	return frontier

def reach(fr, to, i, j, n):
	dist = 0
	level = {fr: dist}
	dist += 1
	frontier = get_frontier(fr, level, i, j, n)
	while frontier:
		nxt = []
		for p in frontier:
			if p not in level:
				level[p] = dist
				if p == to:
					return (dist, level)
				p_frontier = get_frontier(p, level, i, j, n)
				nxt.extend(p_frontier)
		dist += 1
		frontier = nxt
	return (-1, level)

def min_reach(n, i, j):
	(fr, to) = ((0, 0), (n - 1, n - 1))
	(m, level) = reach(fr, to, i, j, n)
	return m

def print_ans(ans):
	for row in ans:
		str_row = ' '.join((str(c) for c in row))
		print(str_row)

def enter_data():
	n = int(input().strip())
	ans = []
	for i in range(1, n):
		row = []
		for j in range(1, n):
			m = min_reach(n, i, j)
			row.append(m)
		ans.append(row)
	return ans
ans = enter_data()
print_ans(ans)


import sys
from itertools import combinations
import collections

def FindMin(n, a, b):
	found = {}
	Combos = [[0, 0]]
	depth = 0
	while len(Combos) > 0:
		newCombos = {}
		for loc in Combos:
			currentKey = '{}x{}'.format(loc[0], loc[1])
			if loc[0] == n - 1 and loc[1] == n - 1:
				return depth
			else:
				found[currentKey] = 1
				newCombos['{}x{}'.format(loc[0] + a, loc[1] + b)] = [loc[0] + a, loc[1] + b]
				newCombos['{}x{}'.format(loc[0] + a, loc[1] - b)] = [loc[0] + a, loc[1] - b]
				newCombos['{}x{}'.format(loc[0] - a, loc[1] + b)] = [loc[0] - a, loc[1] + b]
				newCombos['{}x{}'.format(loc[0] - a, loc[1] - b)] = [loc[0] - a, loc[1] - b]
				newCombos['{}x{}'.format(loc[0] + b, loc[1] + a)] = [loc[0] + b, loc[1] + a]
				newCombos['{}x{}'.format(loc[0] + b, loc[1] - a)] = [loc[0] + b, loc[1] - a]
				newCombos['{}x{}'.format(loc[0] - b, loc[1] + a)] = [loc[0] - b, loc[1] + a]
				newCombos['{}x{}'.format(loc[0] - b, loc[1] - a)] = [loc[0] - b, loc[1] - a]
		depth += 1
		Combos = []
		for key in newCombos:
			combo = newCombos[key]
			if (combo[0] > n - 1 or combo[0] < 0) or (combo[1] > n - 1 or combo[1] < 0):
				continue
			elif key in found:
				continue
			else:
				Combos.append(combo)
	return -1
n = int(input().strip())
output = [[0 for x in range(n - 1)] for y in range(n - 1)]
for i in range(0, n - 1):
	for j in range(i, n - 1):
		output[i][j] = FindMin(n, i + 1, j + 1)
		output[j][i] = output[i][j]
for item in output:
	print(' '.join(map(str, item)))


import collections as col

class CNode:

	def __init__(self, x, y):
		self.x = x
		self.y = y
		self.distance = None
		self.parent = None

	def get_poss_moves(self, a, b, n):
		pmoves = []
		for (i, j) in [(self.x + a, self.y + b), (self.x + b, self.y + a), (self.x - a, self.y + b), (self.x - b, self.y + a), (self.x + a, self.y - b), (self.x + b, self.y - a), (self.x - a, self.y - b), (self.x - b, self.y - a)]:
			if 0 <= i < n and 0 <= j < n:
				if (i, j) not in pmoves:
					pmoves.append((i, j))
		return pmoves

	def __hash__(self):
		return hash((self.x, self.y))

	def __eq__(self, other):
		return self.x == other.x and self.y == other.y

	def __str__(self):
		return str('{},{} : {}'.format(self.x, self.y, self.distance))

class CTree:

	def __init__(self, cnode, a, b, n):
		self.root = cnode
		self.a = a
		self.b = b
		self.n = n
		self.dx = n - 1
		self.dy = n - 1
		self.visited = []

	def do_search(self, node):
		S = set()
		Q = col.deque()
		S.add(node)
		Q.appendleft(node)
		while len(Q) > 0:
			current = Q.pop()
			if current.x == self.dx and current.y == self.dy:
				current.distance = 0
				lpnode = current
				while lpnode is not None:
					if lpnode.parent:
						lpnode.parent.distance = lpnode.distance + 1
						lpnode = lpnode.parent
					else:
						lpnode = None
				return current
			else:
				pmoves = current.get_poss_moves(self.a, self.b, self.n)
				for p in pmoves:
					pnode = CNode(p[0], p[1])
					if pnode not in S:
						S.add(pnode)
						pnode.parent = current
						Q.appendleft(pnode)

def do_walk(x, y, a, b, n):
	cnode = CNode(x, y)
	ctree = CTree(cnode, a, b, n)
	fnode = ctree.do_search(cnode)
	return cnode.distance if cnode.distance is not None else -1
nb = int(input())
for i in range(1, nb):
	rl = []
	for j in range(1, nb):
		rl.append(str(do_walk(0, 0, i, j, nb)))
	print(' '.join(rl))


import sys
n = int(input().strip())

def allmoves(i, j, a, b, n):
	moves = []
	deltas = [(a, b), (a, -b), (-a, b), (-a, -b)]
	deltas.extend([(b, a), (b, -a), (-b, a), (-b, -a)])
	for delta in deltas:
		if i + delta[0] >= 0 and i + delta[0] < n and (j + delta[1] >= 0) and (j + delta[1] < n):
			moves.append((i + delta[0], j + delta[1]))
	return moves

def finddist(n, a, b):
	dist = [[-1 for x in range(n)] for x in range(n)]
	dist[n - 1][n - 1] = 0
	todo = [(n - 1, n - 1)]
	while len(todo) > 0:
		(i, j) = todo.pop(0)
		newmoves = allmoves(i, j, a, b, n)
		for move in newmoves:
			if dist[move[0]][move[1]] == -1:
				dist[move[0]][move[1]] = dist[i][j] + 1
				todo.append((move[0], move[1]))
				if move[0] == 0 and move[1] == 0:
					break
	return dist[0][0]
ans = [[0 for x in range(n - 1)] for x in range(n - 1)]
for i in range(n - 1):
	for j in range(n - 1):
		ans[i][j] = finddist(n, i + 1, j + 1)
for i in range(n - 1):
	print(' '.join(map(str, ans[i])))


import sys
from collections import deque

def knight(n, a, b):
	M = [[-1] * n for i in range(n)]
	M[0][0] = 0
	queue = deque([(0, 0)])
	while queue:
		(x, y) = queue.popleft()
		for i in [x - a, x + a]:
			for j in [y - b, y + b]:
				if 0 <= i < n and 0 <= j < n and (M[i][j] == -1):
					M[i][j] = M[x][y] + 1
					queue.append((i, j))
		for i in [x - b, x + b]:
			for j in [y - a, y + a]:
				if 0 <= i < n and 0 <= j < n and (M[i][j] == -1):
					M[i][j] = M[x][y] + 1
					queue.append((i, j))
	return M[n - 1][n - 1]
n = int(input().strip())
M = [[None] * (n - 1) for i in range(n - 1)]
for i in range(n - 1):
	for j in range(i, n - 1):
		M[i][j] = M[j][i] = knight(n, i + 1, j + 1)
	print(*M[i], sep=' ')


import sys
from collections import deque

def moves(i, j, k, l, n):
	result = []
	result.extend(((i0, j0) for i0 in (i - k, i + k) for j0 in (j - l, j + l) if 0 <= i0 < n and 0 <= j0 < n))
	result.extend(((i0, j0) for i0 in (i - l, i + l) for j0 in (j - k, j + k) if 0 <= i0 < n and 0 <= j0 < n))
	return result

def index(i, j, n):
	return n * i + j

def pos(kk, n):
	return (k // n, k % n)

def main():
	n = int(input().strip())
	min_moves = [-1] * n ** 2
	for k in range(1, n):
		for l in range(k, n):
			board = [-1] * n ** 2
			board[0] = 0
			queue = deque([(0, (0, 0))])
			while queue and queue[-1][1] != (n - 1, n - 1):
				(nmoves, (i, j)) = queue.pop()
				nmoves += 1
				for (i0, j0) in moves(i, j, k, l, n):
					ind = index(i0, j0, n)
					if board[ind] < 0 or board[ind] > nmoves:
						board[ind] = nmoves
						queue.appendleft((nmoves, (i0, j0)))
			min_moves[index(k, l, n)] = min_moves[index(l, k, n)] = board[index(n - 1, n - 1, n)]
	for k in range(1, n):
		print(*min_moves[k * n + 1:k * n + n])
main()


import collections
n = int(input())
grid = [[-1] * (n - 1) for i in range(n - 1)]
for i in range(1, n):
	for j in range(i, n):
		seen = [[0] * n for i in range(n)]
		active = collections.deque()
		active.append((0, n - 1, n - 1))
		while active:
			(s, x, y) = active.popleft()
			if x == 0 and y == 0:
				grid[i - 1][j - 1] = s
				grid[j - 1][i - 1] = s
				break
			if x < 0 or y < 0 or x >= n or (y >= n) or seen[y][x]:
				continue
			seen[y][x] = 1
			seen[x][y] = 1
			s += 1
			active.extend([(s, x + i, y + j), (s, x + i, y - j), (s, x - i, y + j), (s, x - i, y - j), (s, x + j, y + i), (s, x + j, y - i), (s, x - j, y + i), (s, x - j, y - i)])
for row in grid:
	print(' '.join([str(x) for x in row]))