#!/bin/python3 n = int(input().strip()) e = n**2 - 1 def moves(s, a, b, n): """List all possible cells for the next move.""" c = set() xs, ys = s % n, s // n m = [(xs+a, ys+b), (xs+a, ys-b), (xs-a, ys+b), (xs-a, ys-b), (xs+b, ys+a), (xs+b, ys-a), (xs-b, ys+a), (xs-b, ys-a)] for (x, y) in m: if 0 <= x < n and 0 <= y < n: c |= {x + n*y} return c def explore(g, s, a, b, n): """Update the graph with all cells that can be reached from s.""" if s not in g: m = moves(s, a, b, n) g[s] = m for c in m: explore(g, c, a, b, n) def dijkstra(g, s): """Compute the minimal distance from s to any other node.""" q = list(g.keys()) # queue of nodes to visit d = {k: len(q) for k in q} # distance from s d[s] = 0 p = {k: None for k in q} while q != []: # until all nodes have been visited qd = sorted([(n, d[n]) for n in q], key=lambda x: x[1]) u, du = qd[0] q.remove(u) for v in g[u]: if v in q: dv = du + 1 if dv < d[v]: d[v] = dv p[v] = u return d, p def route(p, e): """List nodes making for the shortest path to e.""" path = [] u = e while p[u] is not None: path.insert(0, u) u = p[u] path.insert(0, u) return path for a in range(1, n): for b in range(1, n): g = {} explore(g, 0, a, b, n) if e not in g: r = -1 else: d, p = dijkstra(g, 0) r = len(route(p, e)) - 1 print(r, end=' ') print()