There is a rooted tree (see Notes) with N vertices numbered 1 to N. Each of the vertices, except the root, has a directed edge coming from its parent. Note that the root may not be Vertex 1.  Takahashi has added M new directed edges to this graph. Each of these M edges, u \rightarrow v, extends from some vertex u to its descendant v.  You are given the directed graph with N vertices and N-1+M edges after Takahashi added edges. More specifically, you are given N-1+M pairs of integers, (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the i-th edge extends from Vertex A_i to Vertex B_i.  Restore the original rooted tree.
import sys
input = sys.stdin.buffer.readline
from collections import deque

def main():
    N,M = map(int,input().split())
    edge = [[] for _ in range(N)]
    pcount = [0 for _ in range(N)]
    
    for _ in range(N-1+M):
        a,b = map(int,input().split())
        edge[a-1].append(b-1)
        pcount[b-1] += 1

    for x,num in enumerate(pcount):
        if num == 0:
            root = x
            
    ans = [None for _ in range(N)]
    ans[root] = 0
    q = deque([root])
    while q:
        now = q.popleft()
        for fol in edge[now]:
            pcount[fol] -= 1
            if pcount[fol] == 0:
                ans[fol] = now+1
                q.append(fol)
    
    print(ans)
    
if __name__ == "__main__":
    main()
import sys
input = sys.stdin.buffer.readline
from collections import deque

def main():
    N,M = map(int,input().split())
    edge = [[] for _ in range(N)]
    pcount = [0 for _ in range(N)]
    
    for _ in range(N-1+M):
        a,b = map(int,input().split())
        edge[a-1].append(b-1)
        pcount[b-1] += 1

    for x,num in enumerate(pcount):
        if num == 0:
            root = x
            
    ans = [None for _ in range(N)]
    ans[root] = 0
    q = deque([root])
    while q:
        now = q.popleft()
        for fol in edge[now]:
            pcount[fol] -= 1
            if pcount[fol] == 0:
                ans[fol] = now+1
                q.append(fol)
    
    print(*ans,sep="\n")
    
if __name__ == "__main__":
    main()