INF = 10 ** 15

MOD = 10 ** 9 + 7 

from collections import deque



def main():

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

    G = [[] for _ in range(N)]

    G_inv = [[] for _ in range(N)]

    deg = [0] * N

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

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

        a -= 1

        b -= 1

        deg[b] += 1

        G[a].append(b)

        G_inv[b].append(a)

    for i in range(N):

        if deg[i] == 0:

            root = i

            break

    

    q = deque([root])

    dist = [0] * N

    while q:

        v = q.popleft()

        for e in G[v]:

            dist[e] = dist[v] + 1

            deg[e] -= 1

            if deg[e] == 0:

                q.append(e)



    parents = [0] * N

    for i in range(N):

        if i == root:

            continue

        for e in G_inv[i]:

            if dist[e]  + 1 == dist[i]:

                parents[i] = e + 1

                break



    print(('\n'.join(map(str,parents))))

if __name__ == '__main__':

    main()
import sys

sys.setrecursionlimit(10000000)

MOD = 10 ** 9 + 7

INF = 10 ** 15



from collections import deque,defaultdict,Counter

def main():

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

    G = [[] for _ in range(N)]

    deg = [0] * N

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

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

        a -= 1

        b -= 1

        deg[b] += 1

        G[a].append(b)

    

    ans = [-1] * N

    for i in range(N):

        if deg[i] == 0:

            root = i

            ans[root] = 0

            break

    

    q = deque([root])

    while q:

        v = q.popleft()

        for e in G[v]:

            deg[e] -= 1

            if deg[e] == 0:

                ans[e] = v + 1

                q.append(e)

    print(('\n'.join(map(str,ans))))

if __name__ == '__main__':

    main()
INF = 10**15
MOD = 10**9 + 7
from collections import deque


def main():
    N, M = list(map(int, input().split()))
    G = [[] for _ in range(N)]
    G_inv = [[] for _ in range(N)]
    deg = [0] * N
    for _ in range(N + M - 1):
        a, b = list(map(int, input().split()))
        a -= 1
        b -= 1
        deg[b] += 1
        G[a].append(b)
        G_inv[b].append(a)
    for i in range(N):
        if deg[i] == 0:
            root = i
            break
    q = deque([root])
    dist = [0] * N
    while q:
        v = q.popleft()
        for e in G[v]:
            dist[e] = dist[v] + 1
            deg[e] -= 1
            if deg[e] == 0:
                q.append(e)
    parents = [0] * N
    for i in range(N):
        if i == root:
            continue
        for e in G_inv[i]:
            if dist[e] + 1 == dist[i]:
                parents[i] = e + 1
                break
    print(("\n".join(map(str, parents))))


if __name__ == "__main__":
    main()

import sys

sys.setrecursionlimit(10000000)
MOD = 10**9 + 7
INF = 10**15
from collections import deque, defaultdict, Counter


def main():
    N, M = list(map(int, input().split()))
    G = [[] for _ in range(N)]
    deg = [0] * N
    for _ in range(M + N - 1):
        a, b = list(map(int, input().split()))
        a -= 1
        b -= 1
        deg[b] += 1
        G[a].append(b)
    ans = [-1] * N
    for i in range(N):
        if deg[i] == 0:
            root = i
            ans[root] = 0
            break
    q = deque([root])
    while q:
        v = q.popleft()
        for e in G[v]:
            deg[e] -= 1
            if deg[e] == 0:
                ans[e] = v + 1
                q.append(e)
    print(("\n".join(map(str, ans))))


if __name__ == "__main__":
    main()

+import sys + +sys.setrecursionlimit(10000000) +MOD = 10**9 + 7 -MOD = 10**9 + 7 -from collections import deque +from collections import deque, defaultdict, Counter -    G_inv = [[] for _ in range(N)] -    for _ in range(N + M - 1): +    for _ in range(M + N - 1): -        G_inv[b].append(a) +    ans = [-1] * N +            ans[root] = 0 -    dist = [0] * N -            dist[e] = dist[v] + 1 +                ans[e] = v + 1 -    parents = [0] * N -    for i in range(N): -        if i == root: -            continue -        for e in G_inv[i]: -            if dist[e] + 1 == dist[i]: -                parents[i] = e + 1 -                break -    print(("\n".join(map(str, parents)))) +    print(("\n".join(map(str, ans))))