// Arup Guha
// 3/9/2024
// Solution to Kattis Problem: On Average They're Purple
// Note: Lightly edited Horror solution to solve this.
//       Code I created for that did a multi-source BFS, so I
//       just called that same function with 1 source this time.
// https://open.kattis.com/problems/onaveragetheyrepurple

import java.util.*;

public class onaveragetheyrepurple {

	public static int n;
	public static ArrayList<Integer>[] graph;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		n = stdin.nextInt();
		int numE = stdin.nextInt();
		
		// One source, 0 so we just fill it in.
		int[] start = new int[1];
		
		// Form graph.
		graph = new ArrayList[n];
		for (int i=0; i<n; i++)
			graph[i] = new ArrayList<Integer>();
		
		// Add edges.
		for (int i=0; i<numE; i++) {
			int u = stdin.nextInt()-1;
			int v = stdin.nextInt()-1;
			graph[u].add(v);
			graph[v].add(u);
		}
		
		// Run the BFS. Answer is always 1 less than the actual distance.
		// Color by the BFS all edges from 0 to 1 are red, all from 1 to 2 blue and so forth.
		int[] dist = bfs(start);
		System.out.println(dist[n-1]-1);
	}
	
	// Runs a multi-source BFS from all the vertices listed in sources.
	public static int[] bfs(int[] sources) {
	
		// The queue for my BFS.
		ArrayDeque<Integer> q = new ArrayDeque<Integer>();
		
		// Shortest distances from all the sources.
		int[] dist = new int[n];
		Arrays.fill(dist, -1);
		
		// Add each item to the queue.
		for (int x: sources) {
			q.offer(x);
			dist[x] = 0;
		}
		
		// Keep going until queue is emptied.
		while (q.size() > 0) {
		
			// Remove next item from the queue.
			int cur = q.poll();
			
			// Go to all neighbors of cur.
			for (Integer next: graph[cur]) {
				if (dist[next] != -1) continue;
				
				// Mark distance, add to queue.
				dist[next] = dist[cur] + 1;
				q.offer(next);
			}
		}
	
		return dist;
	}
}