let bfs g n =
  let d = Array.make n (-1) in
  let q = Queue.create () in
  d.(0) <- 0;
  Queue.add 0 q;
  let rec doit () =
    if not (Queue.is_empty q) then begin
      let i = Queue.take q in
      duduwa (d.(i) + 1) g.(i);
      doit ()
    end
  and duduwa x = function
    | [] -> ()
    | hd :: tl when d.(hd-1) = (-1) -> (d.(hd-1) <- x; Queue.add (hd - 1) q; duduwa x tl)
    | _ :: tl -> duduwa x tl
  in doit ();
  d

let () =
  let n = read_int () in
  let g = Array.make n [] in
  let rec read i =
    if i < n then
      match List.map int_of_string (Str.split (Str.regexp " ") (read_line ())) with
      | u :: _ :: l -> (g.(u-1) <- l; read (i + 1))
      | _ -> exit 1
  in read 0;
  let d = bfs g n in
  let rec print i =
    if i < n then (Printf.printf "%d %d\n" (i + 1) d.(i); print (i + 1))
  in print 0