output_description: For each vertex $u$, print $id$ and $d$ in a line. $id$ is ID of vertex $u$
  and $d$ is the distance from vertex $1$ to vertex $u$. If there are no path
  from vertex $1$ to vertex $u$, print -1 as the shortest distance. Print in
  order of IDs.
input_description: In the first line, an integer $n$ denoting the number of vertices, is given.
  In the next $n$ lines, adjacent lists of vertex $u$ are given in the following
  format:
  
  $u$ $k$ $v_1$ $v_2$ ... $v_k$
  
  $u$ is ID of the vertex and $k$ denotes its degree.$v_i$ are IDs of vertices
  adjacent to $u$.
attempt: from collections import deque
  import sys
  
  
  def LI():
      return list(map(int, sys.stdin.buffer.readline().split()))
  
  
  def I():
      return int(sys.stdin.buffer.readline())
  
  
  class Graph:
      def __init__(self, n, is_directed=False):
          """
          :param n: 頂点数
          :param is_directed: 有向グラフ(True)か無向グラフか(False)
          """
          self.n = n
          self.graph = [[] for _ in range(n)]
          self.is_directed = is_directed
  
      def add_edge(self, u, v):
          """uからvへの(有向な)辺をつなぐ"""
          self.graph[u].append(v)
          if not self.is_directed:
              self.graph[v].append(u)
  
      def search(self, start, mode):
          """開始地点から各頂点まで幅/深さ優先探索を行って得られた距離のリストを返す
          Args:
            start(int): 開始地点となる頂点
            mode(str): 幅優先探索(b)か深さ優先探索(d)を指定
  
          Returns:
            List[int]: 開始地点からの距離(未到達の場合は-1)
          """
          dists = [-1] * self.n
          que = deque([start])
          dists[start] = 0
          if mode == "b":
              get_v = que.popleft
          else:
              get_v = que.pop
          while que:
              now_v = get_v()
              for next_v in self.graph[now_v]:
                  if dists[next_v] != -1:
                      continue
                  que.append(next_v)
                  dists[next_v] = dists[now_v] + 1
          return dists
  
  
  n = I()
  g = Graph(n, is_directed=True)
  for _ in range(n):
      u, k, *v = LI()
      u -= 1
      if v == []:
          continue
      for vi in v:
          vi -= 1
          g.add_edge(u, vi)
  res = g.search(0, "b")
  for i in range(n):
      print(i + 1, res[i])
  
problem_description: Breadth First Search
  
  Write a program which reads an directed graph $G = (V, E)$, and finds the
  shortest distance from vertex $1$ to each vertex (the number of edges in the
  shortest path). Vertices are identified by IDs $1, 2, ... n$.
samples: [{"input": "4\n    1 2 2 4\n    2 1 4\n    3 0\n    4 1 3", "output": "1 0\n    2 1\n    3 2\n    4 1"}]