#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 10010;
const int inf = 1e9+7;
int n , m , dis[N] ;
vector<int>g[N];
struct node {
int v , w ;
node(){};
node( int a , int b ) { v =a ,w=b;}
bool operator < ( const node &a ) const {
return w > a.w ;
}
};
int bfs() {
priority_queue<node>que;
for( int i = 0 ; i <= n ; ++i )dis[i] = inf ;
que.push(node(1,0)); dis[1] = 0 ;
while( !que.empty() ) {
node u = que.top() ; que.pop();
if( u.w > dis[u.v] ) continue ;
if( u.v == n ) return dis[u.v] ;
for( int i = 0 ; i < g[u.v].size() ; ++i ) {
int v = g[u.v][i] , w = g[u.v].size() - 1 ;
if( dis[u.v] + w < dis[v] ) {
dis[v] = dis[u.v] + w ;
que.push( node(v,dis[v]) );
}
}
}
return -1;
}
void Run() {
for( int i = 0 ; i <= n ; ++i ) g[i].clear();
while( m-- ) {
int x , y ;
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
printf("%d\n",bfs());
}
int main()
{
// freopen("in","r",stdin);
int _ ,cas =1 ;
while(~scanf("%d%d",&n,&m)) Run();
}