预处理出所有一次能到的节点,然后floyd
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rint register int
ll n,m,mmap[55][55];
bool g[55][55][55];
void f()
{
for(rint k=1;k<=50;k++)
for(rint i=1;i<=n;i++)
for(rint j=1;j<=n;j++)
for(rint l=1;l<=n;l++)
{
if(g[i][j][k-1]&&g[j][l][k-1])
{
g[i][l][k]=true;
mmap[i][l]=1;
}
}
}
void floyd()
{
for(rint k=1;k<=n;k++)
for(rint i=1;i<=n;i++)
for(rint j=1;j<=n;j++)
mmap[i][j]=min(mmap[i][j],mmap[i][k]+mmap[k][j]);
}
int main() {
scanf("%lld%lld",&n,&m);
ll u,v;
memset(mmap,0x3f,sizeof(mmap));
for(rint i=1;i<=m;i++)
{
scanf("%lld%lld",&u,&v);
g[u][v][0]=true;
mmap[u][v]=1;
}
f();
floyd();
printf("%lld
",mmap[1][n]);
return 0;
}