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: class Queue:
      def __init__(self, c):
          self.values = [None] * (c + 1)
          self.s = 0
          self.t = 0
  
      def push(self, x):
          if self._next(self.s) == self.t:
              raise Exception("Overflow")
          self.values[self.s] = x
          self.s = self._next(self.s)
  
      def pop(self):
          if self.s == self.t:
              raise Exception("Underflow")
          ret = self.values[self.t]
          self.t = self._next(self.t)
          return ret
  
      def size(self):
          if self.s >= self.t:
              return self.s - self.t
          else:
              return len(self.values) - (self.t - self.s)
  
      def _next(self, p):
          p += 1
          if p >= len(self.values):
              p = 0
          return p
  
  
  def solve(G):
      d = {}
      n = len(G)
      for u in G:
          d[u] = -1
      d[1] = 0
      queue = Queue(n + 1)
      queue.push(1)
      while queue.size() > 0:
          u = queue.pop()
          for v in G[u]:
              if d[v] == -1:
                  d[v] = d[u] + 1
                  queue.push(v)
      return d
  
  
  n = int(input())
  G = {}
  for i in range(n):
      entry = list(map(int, input().split(" ")))
      u = entry[0]
      k = entry[1]
      G[u] = set()
      for v in entry[2:]:
          G[u].add(v)
  
  
  d = solve(G)
  for u in G:
      print(u, d[u])
  
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"}]