import sys

from collections import defaultdict, deque





def main():

    n, m = list(map(int, sys.stdin.readline().split()))



    out_list = defaultdict(list)

    in_cnt = defaultdict(int)

    for _ in range(n - 1 + m):

        a, b = list(map(int, sys.stdin.readline().split()))

        out_list[a].append(b)

        in_cnt[b] = in_cnt[b] + 1

    ans = [0] * (n + 1)

    que = deque([i for i in range(n) if in_cnt[i] == 0])



    while que:

        p = que.popleft()

        for i in out_list[p]:

            in_cnt[i] = in_cnt[i] - 1

            if in_cnt[i] == 0:

                que.append(i)

                ans[i] = p



    for i in ans[1:]:

        print(i)





if __name__ == "__main__":

    main()
import sys

from collections import deque





def main():

    n, m = list(map(int, sys.stdin.readline().split()))



    out_list = [[] for _ in range(n + 1)]

    in_cnt = [0] * (n + 1)

    in_cnt[0] = -1

    for _ in range(n - 1 + m):

        a, b = list(map(int, sys.stdin.readline().split()))

        out_list[a].append(b)

        in_cnt[b] = in_cnt[b] + 1

    root = in_cnt.index(0)

    ans = [0] * (n + 1)

    que = deque([root])



    while que:

        p = que.popleft()

        for i in out_list[p]:

            in_cnt[i] = in_cnt[i] - 1

            if in_cnt[i] == 0:

                que.append(i)

                ans[i] = p



    for i in ans[1:]:

        print(i)





if __name__ == "__main__":

    main()
import sys
from collections import defaultdict, deque


def main():
    n, m = list(map(int, sys.stdin.readline().split()))
    out_list = defaultdict(list)
    in_cnt = defaultdict(int)
    for _ in range(n - 1 + m):
        a, b = list(map(int, sys.stdin.readline().split()))
        out_list[a].append(b)
        in_cnt[b] = in_cnt[b] + 1
    ans = [0] * (n + 1)
    que = deque([i for i in range(n) if in_cnt[i] == 0])
    while que:
        p = que.popleft()
        for i in out_list[p]:
            in_cnt[i] = in_cnt[i] - 1
            if in_cnt[i] == 0:
                que.append(i)
                ans[i] = p
    for i in ans[1:]:
        print(i)


if __name__ == "__main__":
    main()

import sys
from collections import deque


def main():
    n, m = list(map(int, sys.stdin.readline().split()))
    out_list = [[] for _ in range(n + 1)]
    in_cnt = [0] * (n + 1)
    in_cnt[0] = -1
    for _ in range(n - 1 + m):
        a, b = list(map(int, sys.stdin.readline().split()))
        out_list[a].append(b)
        in_cnt[b] = in_cnt[b] + 1
    root = in_cnt.index(0)
    ans = [0] * (n + 1)
    que = deque([root])
    while que:
        p = que.popleft()
        for i in out_list[p]:
            in_cnt[i] = in_cnt[i] - 1
            if in_cnt[i] == 0:
                que.append(i)
                ans[i] = p
    for i in ans[1:]:
        print(i)


if __name__ == "__main__":
    main()

-from collections import defaultdict, deque +from collections import deque -    out_list = defaultdict(list) -    in_cnt = defaultdict(int) +    out_list = [[] for _ in range(n + 1)] +    in_cnt = [0] * (n + 1) +    in_cnt[0] = -1 +    root = in_cnt.index(0) -    que = deque([i for i in range(n) if in_cnt[i] == 0]) +    que = deque([root])