#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Maxn = 1e6+10;
const int Inf = 0x7f7f7f7f;
const int Mod = 998244353;
int n,m;
char s[12000010];
priority_queue<int,vector<int> ,greater<int> > Map[Maxn];
ll ans[Maxn];
bool vis[Maxn];
void dfs(int s,ll c){
while( !Map[s].empty() )
{
int u = Map[s].top();Map[s].pop();
if( vis[u] ) continue;
ll tmp = (c*1000000 + u)%Mod;
ans[u] = tmp;
vis[u] = true;
dfs(u,tmp);
}
}
int main(){
scanf("%d %d",&n,&m);
if( m == 0 )
{
for(int i=2;i<=n;i++)
puts("-1");
return 0;
}
scanf("%s",s);
int len = strlen(s);
for(int i=0,u=0,v=0;i<len;i+=12)
{
u = 0,v = 0;
for(int j=0;j<6;j++)
u = u*10+s[i+j]-'0';
for(int j=6;j<12;j++)
v = v*10+s[i+j]-'0';
Map[u].push(v);
Map[v].push(u);
}
vis[0] = true;
dfs(0,0);
for(int i=1;i<n;i++)
if( vis[i] ) printf("%lld
",ans[i]);
else puts("-1");
return 0;
}