import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); // your code goes here //System.out.println("n = "+n); int[][]solutie = new int[n][n]; for(int i=1;i exploredSet = new HashSet<>(); ArrayDeque fringe = new ArrayDeque<>(); Node currentNode = new Node(0,0,null); Node solution = null; fringe.add(currentNode); int solutionRow = n-1; while (fringe.size() > 0) { currentNode = fringe.removeFirst(); //nodesEncountered.add(currentNode); exploredSet.add(currentNode); if (currentNode.row == solutionRow && currentNode.column==solutionRow) { solution = currentNode; break; } else { Node tmpNode = null; // UP LEFT if (currentNode.row - i >= 0 && currentNode.column-j>=0) { tmpNode = new Node(currentNode.row - i, currentNode.column-j, currentNode); if (!exploredSet.contains(tmpNode)) { exploredSet.add(tmpNode); fringe.add(tmpNode); } } if (currentNode.row - j >= 0 && currentNode.column-i>=0) { tmpNode = new Node(currentNode.row - j, currentNode.column-i, currentNode); if (!exploredSet.contains(tmpNode)) { exploredSet.add(tmpNode); fringe.add(tmpNode); } } // UP RIGHT if (currentNode.row-i>=0&¤tNode.column + j < n ) { tmpNode = new Node(currentNode.row-i, currentNode.column +j, currentNode); if (!exploredSet.contains(tmpNode)) { exploredSet.add(tmpNode); fringe.add(tmpNode); } } if (currentNode.row-j>=0&¤tNode.column + i < n ) { tmpNode = new Node(currentNode.row-j, currentNode.column +i, currentNode); if (!exploredSet.contains(tmpNode)) { exploredSet.add(tmpNode); fringe.add(tmpNode); } } // DOWN LEFT if (currentNode.row+i=0 ) { tmpNode = new Node(currentNode.row+i, currentNode.column -j, currentNode); if (!exploredSet.contains(tmpNode)) { exploredSet.add(tmpNode); fringe.add(tmpNode); } } if (currentNode.row+j=0 ) { tmpNode = new Node(currentNode.row+j, currentNode.column -i, currentNode); if (!exploredSet.contains(tmpNode)) { exploredSet.add(tmpNode); fringe.add(tmpNode); } } // DOWN RIGHT if (currentNode.row + i < n && currentNode.column +j < n) { tmpNode = new Node(currentNode.row + i, currentNode.column + j, currentNode); if (!exploredSet.contains(tmpNode)) { exploredSet.add(tmpNode); fringe.add(tmpNode); } } if (currentNode.row + j < n && currentNode.column +i < n) { tmpNode = new Node(currentNode.row + j, currentNode.column + i, currentNode); if (!exploredSet.contains(tmpNode)) { exploredSet.add(tmpNode); fringe.add(tmpNode); } } } } if(solution == null){ return -1; } else{ return solution.getRootToNodePath().size()-1; } } static class Node{ private final Node parent; private final int row; public int getRow() { return row; } public int getColumn() { return column; } private final int column; public Node(int row, int column,Node parent) { super(); this.row = row; this.column = column; this.parent = parent; } public Node getParent(){ return parent; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + column; result = prime * result + row; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Node other = (Node) obj; if (column != other.column) return false; if (row != other.row) return false; return true; } public ListgetRootToNodePath(){ ArrayListretList = new ArrayList(); retList.add(this); Node parent = this.parent; while(parent!=null){ retList.add(parent); parent = parent.parent; } Collections.reverse(retList); return retList; } } }