#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg(x) cout<<#x<<" : "<<x<<endl
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define maxn 100005
vector<ll> edges[maxn];
ll dau[maxn],d[maxn];
ll n,m;
void bfs(ll source){
dau[source]=1;
d[source]=0;
queue<ll> q;
q.push(source);
while(!q.empty()){
ll p = q.front();
q.pop();
for(auto v: edges[p]){
if(dau[v]==1) continue;
dau[v]=1;
d[v]=d[p]+1;
q.push(v);
}
}
}
int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin>>n>>m;
for(ll i=0;i<m;i++){
ll u,v,t;
cin>>u>>v>>t;
u--,v--,t--;
edges[u].push_back(v);
edges[u].push_back(t);
edges[v].push_back(u);
edges[t].push_back(u);
}
bfs(0);
for(ll i=0;i<n;i++) cout<<d[i]+1<<'\n';
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcgCiNkZWZpbmUgZGJnKHgpIGNvdXQ8PCN4PDwiIDogIjw8eDw8ZW5kbAojZGVmaW5lIHJlcChpLGEsYikgZm9yKGludCBpPShhKTtpPD0oYik7aSsrKQojZGVmaW5lIG1heG4gMTAwMDA1CnZlY3RvcjxsbD4gZWRnZXNbbWF4bl07CmxsIGRhdVttYXhuXSxkW21heG5dOwpsbCBuLG07CnZvaWQgYmZzKGxsIHNvdXJjZSl7CiAgICBkYXVbc291cmNlXT0xOwogICAgZFtzb3VyY2VdPTA7CiAgICBxdWV1ZTxsbD4gcTsKICAgIHEucHVzaChzb3VyY2UpOwogICAgd2hpbGUoIXEuZW1wdHkoKSl7CiAgICAgICAgbGwgcCA9IHEuZnJvbnQoKTsKICAgICAgICBxLnBvcCgpOwogICAgICAgIGZvcihhdXRvIHY6IGVkZ2VzW3BdKXsKICAgICAgICAgICAgaWYoZGF1W3ZdPT0xKSBjb250aW51ZTsKICAgICA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