
let rec fix f x = f (fix f) x;;
Scanf.(Array.(scanf " %d %d" @@ fun n m ->
  let e = make n [] in
  let e_rev = make n [] in
  init (n-1+m) (fun i -> scanf " %d %d" @@ fun u v ->
    let u,v = u-1,v-1 in
    e.(u) <- v :: e.(u);
    e_rev.(v) <- u :: e_rev.(v)
  ) |> ignore;
  let root = fst @@ fold_left (fun (root,i) ls ->
    (if List.length ls = 0 then i else root),i+1
  ) (0,0) e_rev in
  let dep = make n (-7) in
  fix (fun f i d ->
    if dep.(i) < d then (
      dep.(i) <- max dep.(i) d;
      List.iter (fun j ->
        f j (d+1)
      ) e.(i)
    )
  ) root 0;
  (* iteri (fun i d -> Printf.printf "%d: %d\n" i d) dep; *)
  let res = make n 0 in
  let done_ = make n false in
  fix (fun f i d ->
    List.iter (fun j ->
      if done_.(j) then ()
      else (
        if dep.(j) = d + 1 then (
          res.(j) <- i + 1;
          done_.(j) <- true;
        );
        f j (d+1)
      )
    ) e.(i)
  ) root 0;
  res.(root) <- 0;
  iter (fun d -> Printf.printf "%d\n" d) res;
));;