public static void output(Graph graph, int n) throws Exception { int distance = graph.getDistance(1, n); out.print((distance > n) ? -1 : distance); }