import collections

N = int(input())
M = []
for i in range(N):
    a = list(map(int,input().strip().split()))
    b = [int(i+1 in a[2:]) for i in range(N)]
    M.append(b)

D = [-1 for _ in range(N)]
D[0] = 0 # 始点への距離は 0, 他の距離は-1
Q = collections.deque()
Q.append(0) # 始点
while len(Q) > 0:
    #print("bfs", Q) # 各ステップでの Q の動作を確認
    cur = Q.popleft()
    for dst in range(N):
        # curからdstに移動可能かつ、dstが未訪問だったら
        if M[cur][dst] == 1 and D[dst] == -1:
            D[dst] = D[cur]+1
            Q.append(dst) # Qにdstを詰める
for v in range(N):
    print(v+1, D[v])
 import collections collections N = int(input()) N int(input()) int input() input M = [] M [] for i in range(N):
    a = list(map(int,input().strip().split()))
    b = [int(i+1 in a[2:]) for i in range(N)]
    M.append(b) i range(N) range N a = list(map(int,input().strip().split())) a list(map(int,input().strip().split())) list map(int,input().strip().split()) map int input().strip().split() ().strip().split ().strip() ().strip () input strip split int(i+1 in a[2:]) for i in range(N) for i in range(N) i range(N) range N for i in range(N) int(i+1 in a[2:]) int i+1 in a[2:] i+1 i 1 a[2:] a 2: 2 b = [int(i+1 in a[2:]) for i in range(N)] b [int(i+1 in a[2:]) for i in range(N)] M.append(b) M.append M append b -1 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) -1 D = [-1 for _ in range(N)] D [-1 for _ in range(N)] D[0] = 0 D[0] D 0 0 Q = collections.deque() Q collections.deque() collections.deque collections deque Q.append(0) Q.append Q append 0 while len(Q) > 0:
    #print("bfs", Q) # 各ステップでの Q の動作を確認
    cur = Q.popleft()
    for dst in range(N):
        # curからdstに移動可能かつ、dstが未訪問だったら
        if M[cur][dst] == 1 and D[dst] == -1:
            D[dst] = D[cur]+1
            Q.append(dst) # Qにdstを詰める len(Q) > 0 len(Q) len Q 0 cur = Q.popleft() cur Q.popleft() Q.popleft Q popleft for dst in range(N):
        # curからdstに移動可能かつ、dstが未訪問だったら
        if M[cur][dst] == 1 and D[dst] == -1:
            D[dst] = D[cur]+1
            Q.append(dst) # Qにdstを詰める dst range(N) range N if M[cur][dst] == 1 and D[dst] == -1:
            D[dst] = D[cur]+1
            Q.append(dst) # Qにdstを詰める M[cur][dst] == 1 and D[dst] == -1 M[cur][dst] == 1 M[cur][dst] [cur] M cur dst 1 D[dst] == -1 D[dst] D dst -1 D[dst] = D[cur]+1 D[dst] D dst D[cur]+1 D[cur] D cur 1 Q.append(dst) Q.append Q append dst for v in range(N):
    print(v+1, D[v]) v range(N) range N print(v+1, D[v]) print v+1 v 1 D[v] D v D = [-1 for _ in range(N)] [-1 for _ in range(N)] D N = int(input()) int(input()) N Q = collections.deque() collections.deque() Q M = [] [] M cur = Q.popleft() Q.popleft() cur b = [int(i+1 in a[2:]) for i in range(N)] [int(i+1 in a[2:]) for i in range(N)] b a = list(map(int,input().strip().split())) list(map(int,input().strip().split())) a
import collections

N = int(input())
M = []
for i in range(N):
    a = list(map(int,input().strip().split()))
    b = [int(i+1 in a[2:]) for i in range(N)]
    M.append(b)

D = [-1 for _ in range(N)]
D[0] = 0 # 始点への距離は 0, 他の距離は-1
Q = collections.deque()
Q.append(0) # 始点
while len(Q) > 0:
    #print("bfs", Q) # 各ステップでの Q の動作を確認
    cur = Q.popleft()
    for dst in range(N):
        # curからdstに移動可能かつ、dstが未訪問だったら
        if M[cur][dst] == 1 and D[dst] == -1:
            D[dst] = D[cur]+1
            Q.append(dst) # Qにdstを詰める
for v in range(N):
    print(v+1, D[v])

