#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100005, M = 1000005;
int n, m;
int ecnt, adj[N], nxt[M], go[M], dis[N];
bool vis[N];
queue <int> que;
void add(int u, int v){
go[++ecnt] = v;
nxt[ecnt] = adj[u];
adj[u] = ecnt;
}
void bfs(){
vis[1] = 1;
que.push(1);
while(!que.empty()){
int u = que.front(), v;
que.pop();
for(int e = adj[u]; e; e = nxt[e])
if(!vis[v = go[e]])
dis[v] = dis[u] + 1, vis[v] = 1, que.push(v);
}
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++){
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
}
bfs();
printf("%d\n", dis[n]);
return 0;
}