import collections

n=int(input())
adj=[]
for i in range(n):
      adj.append(list(map(int, input().split())))

Q=[]
Q=collections.deque()
Q.append(0)

D=[-1 for i in range(n)]
D[0]=0

while len(Q)>0:
    current=Q.popleft()
    for dst in range(n):
        for i in range(2,len(adj[current])):
            if adj[current][i]==dst+1 and D[dst]==-1:
                D[dst]=D[current]+1
                Q.append(dst)
for i in range(n):
    print(i+1,D[i]) import collections collections n=int(input()) n int(input()) int input() input adj=[] adj [] for i in range(n):
      adj.append(list(map(int, input().split()))) i range(n) range n adj.append(list(map(int, input().split()))) adj.append adj append list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split Q=[] Q [] Q=collections.deque() Q collections.deque() collections.deque collections deque Q.append(0) Q.append Q append 0 -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)] D[0]=0 D[0] D 0 0 while len(Q)>0:
    current=Q.popleft()
    for dst in range(n):
        for i in range(2,len(adj[current])):
            if adj[current][i]==dst+1 and D[dst]==-1:
                D[dst]=D[current]+1
                Q.append(dst) len(Q)>0 len(Q) len Q 0 current=Q.popleft() current Q.popleft() Q.popleft Q popleft for dst in range(n):
        for i in range(2,len(adj[current])):
            if adj[current][i]==dst+1 and D[dst]==-1:
                D[dst]=D[current]+1
                Q.append(dst) dst range(n) range n for i in range(2,len(adj[current])):
            if adj[current][i]==dst+1 and D[dst]==-1:
                D[dst]=D[current]+1
                Q.append(dst) i range(2,len(adj[current])) range 2 len(adj[current]) len adj[current] adj current if adj[current][i]==dst+1 and D[dst]==-1:
                D[dst]=D[current]+1
                Q.append(dst) adj[current][i]==dst+1 and D[dst]==-1 adj[current][i]==dst+1 adj[current][i] [current] adj current i dst+1 dst 1 D[dst]==-1 D[dst] D dst -1 D[dst]=D[current]+1 D[dst] D dst D[current]+1 D[current] D current 1 Q.append(dst) Q.append Q append dst for i in range(n):
    print(i+1,D[i]) i range(n) range n print(i+1,D[i]) print i+1 i 1 D[i] D i D=[-1 for i in range(n)] [-1 for i in range(n)] D adj=[] [] adj Q=[] [] Q Q=collections.deque() collections.deque() Q n=int(input()) int(input()) n current=Q.popleft() Q.popleft() current
import collections

n=int(input())
adj=[]
for i in range(n):
      adj.append(list(map(int, input().split())))

Q=[]
Q=collections.deque()
Q.append(0)

D=[-1 for i in range(n)]
D[0]=0

while len(Q)>0:
    current=Q.popleft()
    for dst in range(n):
        for i in range(2,len(adj[current])):
            if adj[current][i]==dst+1 and D[dst]==-1:
                D[dst]=D[current]+1
                Q.append(dst)
for i in range(n):
    print(i+1,D[i])