#include <iostream>
#include <cstdlib>
#include <queue>
using namespace std;
int n,m;
queue<int> road[1000];
int ans;
struct node
{
int now;
int length;
};
void bfs()
{
queue<node> q;
node temp, fro;
temp. now=1;
temp. length=0;
q. push (temp);
int i;
while(q.front().now != n)
{
fro = q.front();
q. pop();
i = road [fro.now]. front();
do
{
temp. now = road [fro.now]. front();
temp. length = fro. length + 1;
q. push(temp);
road[fro.now]. push (road[fro. now]. front());
road[fro.now]. pop();
}
while(road[fro.now]. front() != i);
}
ans = q.front(). length;
}
int main()
{
cin >> n >> m;
int i, j;
int from, to;
for(i=1; i<=m; i++)
{
cin >> from >> to;
road[from]. push(to);
}
bfs();
cout << ans << endl;
system("PAUSE");
}
#include <cstdlib>
#include <queue>
using namespace std;
int n,m;
queue<int> road[1000];
int ans;
struct node
{
int now;
int length;
};
void bfs()
{
queue<node> q;
node temp, fro;
temp. now=1;
temp. length=0;
q. push (temp);
int i;
while(q.front().now != n)
{
fro = q.front();
q. pop();
i = road [fro.now]. front();
do
{
temp. now = road [fro.now]. front();
temp. length = fro. length + 1;
q. push(temp);
road[fro.now]. push (road[fro. now]. front());
road[fro.now]. pop();
}
while(road[fro.now]. front() != i);
}
ans = q.front(). length;
}
int main()
{
cin >> n >> m;
int i, j;
int from, to;
for(i=1; i<=m; i++)
{
cin >> from >> to;
road[from]. push(to);
}
bfs();
cout << ans << endl;
system("PAUSE");
}