from collections import deque

import numpy as np

import sys

input = sys.stdin.readline



N, M = map(int, input().split())

AB = [tuple(map(int, input().split())) for _ in range(N + M - 1)]

e_from = [[] for _ in range(N+1)]

e_to = [[] for _ in range(N+1)]

for a, b in AB:

    e_from[a].append(b)

    e_to[b].append(a)





add = np.zeros(N+1, dtype=int)

rank = [-1]*(N+1)



# まず根を探す

for i in range(1, N+1):

    add[i] = len(e_to[i])

    if len(e_to[i]) == 0:

        root = i



# BFS

rank[root] = 0

node = deque([root])

while node:

    s = node.popleft()

    add[e_from[s]] -= 1

    for t in e_from[s]:

        if add[t] == 0:

            rank[t] = s

            node.append(t)



print(*rank[1:], sep="\n")

import sys

input = sys.stdin.readline



N, M = map(int, input().split())

e_out = [[] for _ in range(N + 1)]

e_in = [[] for _ in range(N + 1)]

for _ in range(N + M - 1):

    a, b = map(int, input().split())

    e_out[a].append(b)

    e_in[b].append(a)





# 1. 入次数が0のやつが親

# 2. 親が決まれば、そのこの入次数を減らす,1に戻る



# 親を決めるよ

for i, e in enumerate(e_in[1:], start=1):

    if len(e) == 0:

        root = i

        break



node = [root]

parent = [-1] * (N + 1)

parent[root] = 0

diff = [0] * (N + 1)  # 入次数をどれだけ減らすか



while node:

    s = node.pop()

    for t in e_out[s]:

        diff[t] += 1

        if len(e_in[t]) - diff[t] == 0:

            parent[t] = s

            node.append(t)



print(*parent[1:], sep="\n")

from collections import deque
import numpy as np
import sys

input = sys.stdin.readline
N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N + M - 1)]
e_from = [[] for _ in range(N + 1)]
e_to = [[] for _ in range(N + 1)]
for a, b in AB:
    e_from[a].append(b)
    e_to[b].append(a)
add = np.zeros(N + 1, dtype=int)
rank = [-1] * (N + 1)
# まず根を探す
for i in range(1, N + 1):
    add[i] = len(e_to[i])
    if len(e_to[i]) == 0:
        root = i
# BFS
rank[root] = 0
node = deque([root])
while node:
    s = node.popleft()
    add[e_from[s]] -= 1
    for t in e_from[s]:
        if add[t] == 0:
            rank[t] = s
            node.append(t)
print(*rank[1:], sep="\n")

import sys

input = sys.stdin.readline
N, M = map(int, input().split())
e_out = [[] for _ in range(N + 1)]
e_in = [[] for _ in range(N + 1)]
for _ in range(N + M - 1):
    a, b = map(int, input().split())
    e_out[a].append(b)
    e_in[b].append(a)
# 1. 入次数が0のやつが親
# 2. 親が決まれば、そのこの入次数を減らす,1に戻る
# 親を決めるよ
for i, e in enumerate(e_in[1:], start=1):
    if len(e) == 0:
        root = i
        break
node = [root]
parent = [-1] * (N + 1)
parent[root] = 0
diff = [0] * (N + 1)  # 入次数をどれだけ減らすか
while node:
    s = node.pop()
    for t in e_out[s]:
        diff[t] += 1
        if len(e_in[t]) - diff[t] == 0:
            parent[t] = s
            node.append(t)
print(*parent[1:], sep="\n")

-from collections import deque -import numpy as np -AB = [tuple(map(int, input().split())) for _ in range(N + M - 1)] -e_from = [[] for _ in range(N + 1)] -e_to = [[] for _ in range(N + 1)] -for a, b in AB: -    e_from[a].append(b) -    e_to[b].append(a) -add = np.zeros(N + 1, dtype=int) -rank = [-1] * (N + 1) -# まず根を探す -for i in range(1, N + 1): -    add[i] = len(e_to[i]) -    if len(e_to[i]) == 0: +e_out = [[] for _ in range(N + 1)] +e_in = [[] for _ in range(N + 1)] +for _ in range(N + M - 1): +    a, b = map(int, input().split()) +    e_out[a].append(b) +    e_in[b].append(a) +# 1. 入次数が0のやつが親 +# 2. 親が決まれば、そのこの入次数を減らす,1に戻る +# 親を決めるよ +for i, e in enumerate(e_in[1:], start=1): +    if len(e) == 0: -# BFS -rank[root] = 0 -node = deque([root]) +        break +node = [root] +parent = [-1] * (N + 1) +parent[root] = 0 +diff = [0] * (N + 1)  # 入次数をどれだけ減らすか -    s = node.popleft() -    add[e_from[s]] -= 1 -    for t in e_from[s]: -        if add[t] == 0: -            rank[t] = s +    s = node.pop() +    for t in e_out[s]: +        diff[t] += 1 +        if len(e_in[t]) - diff[t] == 0: +            parent[t] = s -print(*rank[1:], sep="\n") +print(*parent[1:], sep="\n")