#!/usr/bin/env python3
import sys
sys.setrecursionlimit(15000)
n = int(sys.stdin.readline())

ret = []
inp = sys.stdin.readlines()
G = [None]*(n+1)
depths = [-1]*(n+1)

for i in range(1,n+1):
  v = list(map(int,inp[i-1].split()))
  G[v[0]] = list(map(int,v[2:]))
#print(G)

nodes = [1]
depth = 0
while nodes:
  new = []
  for i in nodes:
    if depths[i] == -1:
      depths[i] = depth
      new += G[i]
    #print(i,nodes,depth,depths)
  depth += 1
  nodes = new

for i in range(1,n+1):
  print(i,depths[i]) import sys sys sys.setrecursionlimit(15000) sys.setrecursionlimit sys setrecursionlimit 15000 n = int(sys.stdin.readline()) n int(sys.stdin.readline()) int sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline ret = [] ret [] inp = sys.stdin.readlines() inp sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines G = [None]*(n+1) G [None]*(n+1) [None] None n+1 n 1 depths = [-1]*(n+1) depths [-1]*(n+1) [-1] -1 n+1 n 1 for i in range(1,n+1):
  v = list(map(int,inp[i-1].split()))
  G[v[0]] = list(map(int,v[2:]))
#print(G) i range(1,n+1) range 1 n+1 n 1 v = list(map(int,inp[i-1].split())) v list(map(int,inp[i-1].split())) list map(int,inp[i-1].split()) map int inp[i-1].split() [i-1].split [i-1] inp i-1 i 1 split G[v[0]] = list(map(int,v[2:])) G[v[0]] G v[0] v 0 list(map(int,v[2:])) list map(int,v[2:]) map int v[2:] v 2: 2 nodes = [1] nodes [1] 1 depth = 0 depth 0 while nodes:
  new = []
  for i in nodes:
    if depths[i] == -1:
      depths[i] = depth
      new += G[i]
    #print(i,nodes,depth,depths)
  depth += 1
  nodes = new nodes new = [] new [] for i in nodes:
    if depths[i] == -1:
      depths[i] = depth
      new += G[i]
    #print(i,nodes,depth,depths)
   i nodes if depths[i] == -1:
      depths[i] = depth
      new += G[i]
    #print(i,nodes,depth,depths)
   depths[i] == -1 depths[i] depths i -1 depths[i] = depth depths[i] depths i depth new += G[i] new G[i] G i depth += 1 depth 1 nodes = new nodes new for i in range(1,n+1):
  print(i,depths[i]) i range(1,n+1) range 1 n+1 n 1 print(i,depths[i]) print i depths[i] depths i ret = [] [] ret inp = sys.stdin.readlines() sys.stdin.readlines() inp G = [None]*(n+1) [None]*(n+1) G n = int(sys.stdin.readline()) int(sys.stdin.readline()) n new = [] [] new new += G[i] G[i] new depth = 0 0 depth depth += 1 1 depth nodes = [1] [1] nodes depths = [-1]*(n+1) [-1]*(n+1) depths v = list(map(int,inp[i-1].split())) list(map(int,inp[i-1].split())) v nodes = new new nodes
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(15000)
n = int(sys.stdin.readline())

ret = []
inp = sys.stdin.readlines()
G = [None]*(n+1)
depths = [-1]*(n+1)

for i in range(1,n+1):
  v = list(map(int,inp[i-1].split()))
  G[v[0]] = list(map(int,v[2:]))
#print(G)

nodes = [1]
depth = 0
while nodes:
  new = []
  for i in nodes:
    if depths[i] == -1:
      depths[i] = depth
      new += G[i]
    #print(i,nodes,depth,depths)
  depth += 1
  nodes = new

for i in range(1,n+1):
  print(i,depths[i])
