n = input()
  
d = [-1 for i in range(n)]
M = [[0 for i in range(n)] for j in range(n)]
color = [0 for i in range(n)]
  
def BFS(s):
  color[0] = 1
  q = []
  q.append(s)
  d[s] = 0
  while len(q) != 0:
    u = q[0]
    q.pop(0)
    for v in range(n):
      if M[u][v] == 1 and color[v] == 0:
        color[v] = 1
        d[v] = d[u] + 1
        q.append(v)
  color[u] = 2
  for i in range(n):
    print i+1, d[i]
  
for i in range(n):
  G = map(int, raw_input().split())
  for j in range(G[1]):
    M[G[0]-1][G[2+j]-1] = 1
  
BFS(0) n = input() n input() input -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 d = [-1 for i in range(n)] d [-1 for i in range(n)] [0 for i in range(n)] for j in range(n) for j in range(n) j range(n) range n for j in range(n) [0 for i in range(n)] M = [[0 for i in range(n)] for j in range(n)] M [[0 for i in range(n)] for j in range(n)] 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 color = [0 for i in range(n)] color [0 for i in range(n)] def BFS(s):
  color[0] = 1
  q = []
  q.append(s)
  d[s] = 0
  while len(q) != 0:
    u = q[0]
    q.pop(0)
    for v in range(n):
      if M[u][v] == 1 and color[v] == 0:
        color[v] = 1
        d[v] = d[u] + 1
        q.append(v)
  color[u] = 2
  for i in range(n):
    print i+1, d[i]
   BFS color[0] = 1 color[0] color 0 1 q = [] q [] q.append(s) q.append q append s d[s] = 0 d[s] d s 0 while len(q) != 0:
    u = q[0]
    q.pop(0)
    for v in range(n):
      if M[u][v] == 1 and color[v] == 0:
        color[v] = 1
        d[v] = d[u] + 1
        q.append(v)
   len(q) != 0 len(q) len q 0 u = q[0] u q[0] q 0 q.pop(0) q.pop q pop 0 for v in range(n):
      if M[u][v] == 1 and color[v] == 0:
        color[v] = 1
        d[v] = d[u] + 1
        q.append(v)
   v range(n) range n if M[u][v] == 1 and color[v] == 0:
        color[v] = 1
        d[v] = d[u] + 1
        q.append(v)
   M[u][v] == 1 and color[v] == 0 M[u][v] == 1 M[u][v] [u] M u v 1 color[v] == 0 color[v] color v 0 color[v] = 1 color[v] color v 1 d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 q.append(v) q.append q append v color[u] = 2 color[u] color u 2 for i in range(n):
    print i+1, d[i]
   i range(n) range n print i+1 i 1 d[i] d i s s for i in range(n):
  G = map(int, raw_input().split())
  for j in range(G[1]):
    M[G[0]-1][G[2+j]-1] = 1
   i range(n) range n G = map(int, raw_input().split()) G map(int, raw_input().split()) map int raw_input().split() ().split () raw_input split for j in range(G[1]):
    M[G[0]-1][G[2+j]-1] = 1
   j range(G[1]) range G[1] G 1 M[G[0]-1][G[2+j]-1] = 1 M[G[0]-1][G[2+j]-1] [G[0]-1] M G[0]-1 G[0] G 0 1 G[2+j]-1 G[2+j] G 2+j 2 j 1 1 BFS(0) BFS 0 M = [[0 for i in range(n)] for j in range(n)] [[0 for i in range(n)] for j in range(n)] M color = [0 for i in range(n)] [0 for i in range(n)] color d = [-1 for i in range(n)] [-1 for i in range(n)] d G = map(int, raw_input().split()) map(int, raw_input().split()) G n = input() input() n def BFS(s):
  color[0] = 1
  q = []
  q.append(s)
  d[s] = 0
  while len(q) != 0:
    u = q[0]
    q.pop(0)
    for v in range(n):
      if M[u][v] == 1 and color[v] == 0:
        color[v] = 1
        d[v] = d[u] + 1
        q.append(v)
  color[u] = 2
  for i in range(n):
    print i+1, d[i]
   def BFS(s):
  color[0] = 1
  q = []
  q.append(s)
  d[s] = 0
  while len(q) != 0:
    u = q[0]
    q.pop(0)
    for v in range(n):
      if M[u][v] == 1 and color[v] == 0:
        color[v] = 1
        d[v] = d[u] + 1
        q.append(v)
  color[u] = 2
  for i in range(n):
    print i+1, d[i]
   BFS
n = input()
  
d = [-1 for i in range(n)]
M = [[0 for i in range(n)] for j in range(n)]
color = [0 for i in range(n)]
  
def BFS(s):
  color[0] = 1
  q = []
  q.append(s)
  d[s] = 0
  while len(q) != 0:
    u = q[0]
    q.pop(0)
    for v in range(n):
      if M[u][v] == 1 and color[v] == 0:
        color[v] = 1
        d[v] = d[u] + 1
        q.append(v)
  color[u] = 2
  for i in range(n):
    print i+1, d[i]
  
for i in range(n):
  G = map(int, raw_input().split())
  for j in range(G[1]):
    M[G[0]-1][G[2+j]-1] = 1
  
BFS(0)