n,m = map(int,input().split()) assert 2<=n<=1000 assert 1<=m<=min(n*(n-1)//2,2500) edge = [[] for i in range(n)] edge_cost = {} edge_weight = [] check = set([]) for i in range(m): u,v,w = map(int,input().split()) edge[u-1].append(v-1) edge_cost[u-1,v-1] = 1 edge_weight.append((u-1,v-1,w)) assert 1<=u<=n assert 1<=v<=n assert 1<=w<=10**9 assert (u,v) not in check assert u!=v check.add((u,v)) dist = [10**18 for i in range(n)] ans = [-1 for i in range(n)] dist[0] = 0 def dfs(s,w): que = [s] while que: v = que.pop() for nv in edge[v]: if dist[v]!=-10**18: if dist[nv] > dist[v] + edge_cost[v,nv]: dist[nv] = dist[v] + edge_cost[v,nv] if dist[nv] < -n: dist[nv] = -10**18 if dist[nv]<=0 and ans[nv]==-1: ans[nv] = w que.append(nv) else: if dist[nv]!=-10**18: dist[nv] = -10**18 if ans[nv]==-1: ans[nv] = w que.append(nv) dfs(0,-1) edge_weight.sort(key = lambda x:x[2]) for u,v,w in edge_weight: edge_cost[u,v] = -1 if dist[v] > dist[u] + edge_cost[u,v]: dist[v] = dist[u] + edge_cost[u,v] if dist[v] < -n: dist[v] = -10**18 if dist[v]<=0 and ans[v]==-1: ans[v] = w dfs(v,w) print(*ans[1:],sep="\n")