import sys readline = sys.stdin.readline MOD = 10**9+7 N, M = map(int, readline().split()) cEdge = [] Edge = [[] for _ in range(N)] for _ in range(M): u, v, w = map(int, readline().split()) u -= 1 v -= 1 cEdge.append((w, u, v)) Edge[u].append((w, v)) cEdge.sort() used = set() ans = [-1]*N block = 2* + 10 geta = N + 3 used.add(geta) for w, u, v in cEdge: stack = [geta] for c in range(block-1): unode = u*block + c vnode = v*block + c + 1 if unode in used and vnode not in used: used.add(vnode) stack.append(vnode) if c + 1 >= geta and ans[v] == -1: ans[v] = w for c in range(1, block): unode = u*block + c vnode = v*block + c - 1 if unode in used and vnode not in used: used.add(vnode) stack.append(vnode) if c - 1 >= geta and ans[v] == -1: ans[v] = w while stack: node = stack.pop() vn, c = divmod(node, block) for wi, vf in Edge[vn]: if wi <= w: if c + 1 < block: vfnode = vf*block + c + 1 if vfnode not in used: used.add(vfnode) stack.append(vfnode) if ans[vf] == -1 and c+1 >= geta: ans[vf] = w else: if c - 1 >= 0: vfnode = vf*block + c - 1 if vfnode not in used: used.add(vfnode) stack.append(vfnode) if ans[vf] == -1 and c-1 >= geta: ans[vf] = w print('\n'.join(map(str, ans[1:])))