#include<bits/stdc++.h>
using namespace std ;
const int N = 2e5+5 ;
const int M = 5e5+5 ;
int n , m , dis[N] , vis[N] , ans[N] , flag[N] ;
vector<int> nei[N] , renei[N] ;
void bfs(int node){
queue<int> q ;
memset(dis,0x3f,sizeof(dis)) ;
q.push(node) ;
dis[node]=0 ; vis[node]=1 ;
while(!q.empty()){
int k = q.front() ; q.pop() ;
for(int i:nei[k]){
if(vis[i]) continue ;
dis[i]=dis[k]+1 ;
q.push(i) ; vis[i]=1 ;
}
}
}
void weibfs(int node){
priority_queue<pair<int,int>> pq ;
memset(ans,0x3f,sizeof(ans)) ;
for(int i:renei[node]){
ans[i]=dis[i] , pq.push({-ans[i],i}) ;
if(!i) ans[i]=1 ;
}
while(!pq.empty()){
int w = -pq.top().first , k = pq.top().second ; pq.pop() ;
if(ans[k]<w) continue ;
for(int i:renei[k]){
if(ans[i]<=max(dis[i],ans[k]+1)) continue ;
ans[i]=max(dis[i],ans[k]+1) ;
pq.push({-ans[i],i}) ;
}
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) ;
cin >> n >> m ;
for(int i=0 ; i<m ; i++){
int a , b ; cin >> a >> b ;
nei[a].push_back(b) ; renei[b].push_back(a) ;
}
bfs(0) ;
weibfs(n-1) ;
for(int i=0 ; i<n-1 ; i++) cout << ans[i] << ' ' ;
cout << '\n' ;
return 0 ;
}