let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let bfs vs n =
  let d = Array.make (n + 1) (-1) in
  d.(1) <- 0;
  let que = Queue.create () in
  Queue.add 1 que;
  while not (Queue.is_empty que) do
    let i = Queue.take que in
    List.iter (fun e ->
      if d.(e) = (-1) then begin
        d.(e) <- d.(i) + 1;
        Queue.add e que;
      end) vs.(i);
  done;
  d

let () =
  let n = read_int () in
  let vs = Array.make (n + 1) [] in
  for _ = 0 to n - 1 do
    match read_line () |> split_on_char ' ' |> List.map int_of_string with
    | u :: _ :: l -> vs.(u) <- l
    | _ -> assert false
  done;
  let d = bfs vs n in
  for i = 1 to n do
    Printf.printf "%d %d\n" i d.(i)
  done