/*
AUTHOR : Chandan Agrawal
College : Poornima College of Engg. jaipur, Raj
Mail : chandanagrawal23@gmail.com
*/
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define MAX 2000050
#define ll long long
#define ld long double
#define lli long long int
#define pb emplace_back
#define INF 1000000000
#define mod 1000000007
// trignometric function always give value in Radians only
#define PI acos(-1) //3.1415926535897932384626433832795028
#define dsin(degree) sin(degree*(PI/180.0))
#define dcos(degree) cos(degree*(PI/180.0))
#define dtan(degree) tan(degree*(PI/180.0))
#define rsin(radian) sin(radian)
#define rcos(radian) cos(radian)
#define rtan(radian) tan(radian)
#define loop(i,n) for (lli i = 0; i < n; i++)
#define loopitr(xt,vec) for (auto xt : vec)
#define FOR(i,a,b) for (lli i = a; i < b; i+=1)
#define loop_rev(i,n) for (lli i = n-1; i >= 0; i--)
#define FOR_REV(i,a,b) for (lli i = a; i >= b; i--)
#define itr :: iterator it
#define WL(t) while(t --)
#define all(v) v.begin(),v.end()
#define sz(x) int(x.size())
#define F first
#define S second
#define mii map<lli,lli>
#define vi vector<lli>
#define seti set<lli>
#define pii pair<lli,lli>
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (a/gcd(a,b))*b
#define abs(x) ((x < 0)?-(x):x)
template <typename T>
void print(T x){cout<<x<<endl;}
template <typename T1, typename T2>
void print2(T1 x,T2 y){cout<<x<<" "<<y<<endl;}
template <typename T1, typename T2,typename T3>
void print3(T1 x, T2 y,T3 z){cout<<x<<" "<<y<<" "<<z<<endl;}
#define scanarr(a,n) for(lli i=0;i<n;i++) cin>>a[i];
#define scanvector(a,n) for(lli i=0;i<n;i++){ lli x ; cin>>x; a.pb(x);}
#define printarr(a,n) for(lli i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define printvector(vec) for(auto xt : vec) cout<<xt<<" "; cout<<"\n";
#define printset(st) for(auto xt : st) cout<<xt<<" "; cout<<"\n";
#define FD(N) fixed<<setprecision(N)
#define endl '\n'
#define deb(x) cout<<#x<<" "<<x<<endl;
// chandan1,2
void chandan1(){int y=1;return;}
void chandan2(){
loop(i,10){
lli x=1;
}
return(chandan1());
}
/*
1) Initialize distances of all vertices as infinite.
2) Create an empty priority_queue pq. Every item
of pq is a pair (weight, vertex). Weight (or
distance) is used used as first item of pair
as first item is by default used to compare
two pairs
3) Insert source vertex into pq and make its
distance as 0.
4) While either pq doesn't become empty
a) Extract minimum distance vertex from pq.
Let the extracted vertex be u.
b) Loop through all adjacent of u and do
following for every vertex v.
// If there is a shorter path to v
// through u.
If dist[v] > dist[u] + weight(u, v)
(i) Update distance of v, i.e., do
dist[v] = dist[u] + weight(u, v)
(ii) Insert v into the pq (Even if v is
already there)
5) Print distance array dist[] to print all shortest
paths.
*/
// create adjacency list of pair
// adj[u].pb({v,cost}) from u to v cost is cost
vector<pii>adj[MAX];
lli dist[MAX];
bool flags[MAX];
lli Dijikistra(lli v , lli source){
for(lli i=1;i<=v;i++)
dist[i] = INF;
dist[source] = 0;
// min priority queue of pair
// where pair is < (cost from u to v) , node >
priority_queue<pii , vector<pii> , greater<pii> > pq;
pq.push(make_pair(0,source)); // from source to source cost is 0
while(!pq.empty()){
lli u = pq.top().S;
pq.pop();
for(auto xt : adj[u]){
lli v = xt.F;
lli weight = xt.S;
if(dist[v] > (dist[u] + weight) )
{
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v] , v));
}
}
}
return( dist[v] );
}
int main(){
fastio
lli t=1;
//cin>>t;
chandan2();
while(t--) {
lli v,e;
cin>>v>>e;
loop(i,e){
lli x,y;
cin>>x>>y;
if(x!=y){
adj[x].pb(make_pair(y,0)); // jo already connected hai unko 0 cost;
adj[y].pb(make_pair(x,1)); // now reverse edge with cost 1
}
}
lli ans = Dijikistra(v,1);
if(ans == INF)
print(-1);
else
print(ans);
}
return 0;
}