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 topological_sort root =
    let state = make n `Todo in
    let res = ref [] in
    let rec visit i = match state.(i) with
    | `Done -> true
    | `Temp -> false (* cycle detected *)
    | `Todo -> (
      state.(i) <- `Temp;
      let f =
        List.fold_left (fun u j ->
          u && visit j
        ) true e.(i)
      in
      state.(i) <- `Done;
      res := i :: !res; f
    ) in
    (* if
      fold_left (fun u i ->
        if state.(i) = `Todo then
          u && visit i
        else u
      ) true @@ init n (fun i -> i)
    then Some !res else None *)
    assert (visit root); !res
  in
  let tp = topological_sort root in
  let ord = make n 0 in
  List.iteri (fun i v -> ord.(v) <- i) tp;
  let res = init n (fun i ->
    if i = root then 0
    else 1 + (
      e_rev.(i)
      |> List.sort (fun u v -> -compare ord.(u) ord.(v))
      |> List.hd)
  ) in
  iter (Printf.printf "%d ") res
))