#!/bin/python3 import sys n = int(input().strip()) def getMoves(x,y,i): moves =[] m1 = i+x + n*y m2 = i+x - n*y m3 = i-x + n*y m4 = i-x - n*y m5 = i+y + n*x m6 = i+y - n*x m7 = i-y + n*x m8 = i-y - n*x #Moving left if i % n >= x: #up if y <= int(i/n): moves.append(m4) #down if y +int(i/n) < n: moves.append(m3) if i % n >= y: #up if x <= int(i/n): moves.append(m8) #down if x +int(i/n) < n: moves.append(m7) #Moving right if (i % n) +x < n: if y <= int(i/n): moves.append(m2) if y +int(i/n) < n: moves.append(m1) if (i % n) + y < n: if x <= int(i/n): moves.append(m6) if x +int(i/n) < n: moves.append(m5) return moves def shortestPath (start,end,x,y): found = False queue = [start] prev = {start:-1} visited = set() while len(queue) > 0: c = queue.pop(0) moves = getMoves(x,y,c) visited.add(c) if (end in moves): found = True prev[end] = c break else: for u in moves: if u not in visited and u not in queue: queue.append(u) prev[u] = c if found: count = 0 path =[end] x = end while x != -1: x = prev[x] path.append(x) count += 1 return count - 1 else: return -1 # your code goes here start = 0 end = n*n-1 answer = [] for y in range(n): answer.append([-1]*n) for y in range(n-1): for x in range(n -1): answer[x][y] = shortestPath(start,end,x+1,y+1) print(answer[x][y], end=' ') print("")