
n = input()
d = [99999 for i in range(n)]
G = [0 for i in range(n)]
v = [[0 for i in range(n)] for j in range(n)]
   
def bfs(u):
  for i in range(n):
    if v[u][i] == 1:
      if d[i] > d[u] + 1:
         d[i] = d[u] + 1
         bfs(i)
    else:
      continue        
def _(): 
 d[0] = 0
 for i in range(n):
   bfs(i)
 for i in range(n):
   if d[i] == 99999:
      d[i] = -1
 for i in range(n):
   print '{0} {1}'.format(i+1, d[i])


for i in range(n):
  G = map(int, raw_input().split())
  for j in range(G[1]):
    v[G[0]-1][G[2+j]-1] = 1

_() n = input() n input() input 99999 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 99999 d = [99999 for i in range(n)] d [99999 for i 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 G = [0 for i in range(n)] G [0 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)] v = [[0 for i in range(n)] for j in range(n)] v [[0 for i in range(n)] for j in range(n)] def bfs(u):
  for i in range(n):
    if v[u][i] == 1:
      if d[i] > d[u] + 1:
         d[i] = d[u] + 1
         bfs(i)
    else:
      continue         bfs for i in range(n):
    if v[u][i] == 1:
      if d[i] > d[u] + 1:
         d[i] = d[u] + 1
         bfs(i)
    else:
      continue         i range(n) range n if v[u][i] == 1:
      if d[i] > d[u] + 1:
         d[i] = d[u] + 1
         bfs(i)
    else:
      continue         v[u][i] == 1 v[u][i] [u] v u i 1 if d[i] > d[u] + 1:
         d[i] = d[u] + 1
         bfs(i)
     d[i] > d[u] + 1 d[i] d i d[u] + 1 d[u] d u 1 d[i] = d[u] + 1 d[i] d i d[u] + 1 d[u] d u 1 bfs(i) bfs i continue u u def _(): 
 d[0] = 0
 for i in range(n):
   bfs(i)
 for i in range(n):
   if d[i] == 99999:
      d[i] = -1
 for i in range(n):
   print '{0} {1}'.format(i+1, d[i]) _ d[0] = 0 d[0] d 0 0 for i in range(n):
   bfs(i)
  i range(n) range n bfs(i) bfs i for i in range(n):
   if d[i] == 99999:
      d[i] = -1
  i range(n) range n if d[i] == 99999:
      d[i] = -1
  d[i] == 99999 d[i] d i 99999 d[i] = -1 d[i] d i -1 for i in range(n):
   print '{0} {1}'.format(i+1, d[i]) i range(n) range n print '{0} {1}'.format(i+1, d[i]) '{0} {1}'.format '{0} {1}' format i+1 i 1 d[i] d i for i in range(n):
  G = map(int, raw_input().split())
  for j in range(G[1]):
    v[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]):
    v[G[0]-1][G[2+j]-1] = 1 j range(G[1]) range G[1] G 1 v[G[0]-1][G[2+j]-1] = 1 v[G[0]-1][G[2+j]-1] [G[0]-1] v G[0]-1 G[0] G 0 1 G[2+j]-1 G[2+j] G 2+j 2 j 1 1 _() _ G = map(int, raw_input().split()) map(int, raw_input().split()) G def bfs(u):
  for i in range(n):
    if v[u][i] == 1:
      if d[i] > d[u] + 1:
         d[i] = d[u] + 1
         bfs(i)
    else:
      continue         def bfs(u):
  for i in range(n):
    if v[u][i] == 1:
      if d[i] > d[u] + 1:
         d[i] = d[u] + 1
         bfs(i)
    else:
      continue         bfs n = input() input() n d = [99999 for i in range(n)] [99999 for i in range(n)] d def _(): 
 d[0] = 0
 for i in range(n):
   bfs(i)
 for i in range(n):
   if d[i] == 99999:
      d[i] = -1
 for i in range(n):
   print '{0} {1}'.format(i+1, d[i]) def _(): 
 d[0] = 0
 for i in range(n):
   bfs(i)
 for i in range(n):
   if d[i] == 99999:
      d[i] = -1
 for i in range(n):
   print '{0} {1}'.format(i+1, d[i]) _ G = [0 for i in range(n)] [0 for i in range(n)] G v = [[0 for i in range(n)] for j in range(n)] [[0 for i in range(n)] for j in range(n)] v

n = input()
d = [99999 for i in range(n)]
G = [0 for i in range(n)]
v = [[0 for i in range(n)] for j in range(n)]
   
def bfs(u):
  for i in range(n):
    if v[u][i] == 1:
      if d[i] > d[u] + 1:
         d[i] = d[u] + 1
         bfs(i)
    else:
      continue        
def _(): 
 d[0] = 0
 for i in range(n):
   bfs(i)
 for i in range(n):
   if d[i] == 99999:
      d[i] = -1
 for i in range(n):
   print '{0} {1}'.format(i+1, d[i])


for i in range(n):
  G = map(int, raw_input().split())
  for j in range(G[1]):
    v[G[0]-1][G[2+j]-1] = 1

_()