using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray(); static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var map = NMap(m); var tree = new List[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List(); foreach (var edge in map) { tree[edge[0]].Add(edge[1]); tree[edge[1]].Add(edge[0]); } var INF = int.MaxValue / 2; var len = Enumerable.Repeat(INF, n).ToArray(); len[0] = 0; var q = new Queue<(int pos, int len)>(); q.Enqueue((0, 0)); while (q.Count > 0) { var cur = q.Dequeue(); foreach (var next in tree[cur.pos]) { if (len[next] <= cur.len + 1) continue; len[next] = cur.len + 1; q.Enqueue((next, len[next])); } } WriteLine(len[^1] == INF ? -1 : len[^1]); } }