Scanf.scanf "%d %d" (fun n m ->
    let nei = Array.make n [] in
    let rev = Array.make n 0 in
    for i = 1 to n - 1 + m do
        Scanf.scanf " %d %d" (fun a b ->
            let a = a - 1 in
            let b = b - 1 in
            nei.(a) <- b :: nei.(a);
            rev.(b) <- rev.(b) + 1
        )
    done;
    let rec loop i =
        if rev.(i) = 0 then i else loop (i + 1)
    in
    let root = loop 0 in
    let par = Array.make n 0 in

    let rec loop next = function
        | [] -> if next <> [] then loop [] next
        | hd :: tl ->
            let rec loop2 next = function
                | [] -> loop next tl
                | x :: xs ->
                        rev.(x) <- rev.(x) - 1;
                        if rev.(x) > 0 then loop2 next xs else (
                            par.(x) <- hd + 1;
                            loop2 (x :: next) xs
                        )
            in
            loop2 next nei.(hd)
    in
    loop [] [ root ];
    Array.iter (Printf.printf "%d\n") par
)