#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#define N 100010
using namespace std;
int n,m,dis[N];
vector <int> adj[N];
queue <int> q;
int bfs()
{
memset(dis,-1,sizeof(dis));
dis[1]=0;
q.push(1);
while (!q.empty())
{
int u=q.front();
if (u==n) break;
q.pop();
for (int i=0;i<adj[u].size();i++)
if (!~dis[adj[u][i]])
dis[adj[u][i]]=dis[u]+1,q.push(adj[u][i]);
}
return dis[n];
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1,u,v;i<=m;i++)
{
scanf("%d%d",&u,&v);
adj[u].push_back(v);
}
printf("%d",bfs());
return 0;
}