using System.Linq; using System.Collections.Generic; using System; public class P { public int to { get; set; } public int step { get; set; } } public class Hello { public static int n, m; static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); n = int.Parse(line[0]); m = int.Parse(line[1]); var aa = new List[n]; for (int i = 0; i < n; i++) aa[i] = new List(); for (int i = 0; i < m; i++) { line = Console.ReadLine().Trim().Split(' '); var a = int.Parse(line[0]) - 1; var b = int.Parse(line[1]) - 1; aa[a].Add(b); aa[b].Add(a); } getAns(aa); } static void getAns(List[] aa) { var q = new Queue

(); var visited = new bool[n]; q.Enqueue(new P { to = 0, step = 0 }); visited[0] = true; while (q.Count() > 0) { var t = q.Dequeue(); if (t.to == n - 1) { Console.WriteLine(t.step); return; } foreach (var x in aa[t.to]) { if (visited[x]) continue; q.Enqueue(new P { to = x, step = t.step + 1 }); visited[x] = true; } } Console.WriteLine(-1); } }