import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static class KnightL { final int a; final int b; KnightL(int a, int b) { this.a = a; this.b = b; } } static class State { final Position position; final int moves; State(Position position, int moves) { this.position = position; this.moves = moves; } } static class Position { final int x; final int y; Position(int x, int y) { this.x = x; this.y = y; } private boolean isLegal(int n) { return x >= 0 && x < n && y >= 0 && y < n; } public boolean isFinal(int n) { return x == n - 1 && y == n - 1; } public List move(KnightL knightL, int n) { int a = knightL.a; int b = knightL.b; List generated = new LinkedList<>(); generated.add(new Position(x + a, y + b)); generated.add(new Position(x - a, y + b)); generated.add(new Position(x + a, y - b)); generated.add(new Position(x - a, y - b)); generated.add(new Position(x + b, y + a)); generated.add(new Position(x - b, y + a)); generated.add(new Position(x + b, y - a)); generated.add(new Position(x - b, y - a)); for (Iterator iter = generated.iterator(); iter.hasNext();) { final Position pos = iter.next(); if (!pos.isLegal(n)) { iter.remove(); } } return generated; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Position position = (Position) o; return x == position.x && y == position.y; } @Override public int hashCode() { return Objects.hash(x, y); } } static void solveGivenKnightL(KnightL knightL, int n) { Queue toExplore = new LinkedList<>(); toExplore.offer(new State(new Position(0, 0), 0)); Set alreadyVisited = new HashSet<>(); boolean found = false; while (!toExplore.isEmpty()) { final State curr = toExplore.poll(); if (alreadyVisited.contains(curr.position)) { continue; } if (curr.position.isFinal(n)) { System.out.print(curr.moves + " "); found = true; break; } alreadyVisited.add(curr.position); final List generated = curr.position.move(knightL, n); for (Position gen: generated) { if (!alreadyVisited.contains(gen)) { toExplore.offer(new State(gen, curr.moves + 1)); } } } if (!found) { System.out.print("-1 "); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); // your code goes here for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { KnightL knightL = new KnightL(i, j); solveGivenKnightL(knightL, n); } System.out.println(); } } }