from collections import deque


def BFS(num):
    color=["white" for _ in range(n+1)]
    D=[-1 for _ in range(n+1)]

    color[num]="gray"
    D[num]=0
    queue=deque([num])

    while len(queue)>0:
        u=queue.popleft()
        for i in M[u]:
            if color[i]=="white":
                color[i]="gray"
                D[i]=D[u]+1
                queue.append(i)
        color[u]="black"

    return D


n=int(input())

M=[[] for _ in range(n+1)]
for i in range(n):
    for j in list(map(int,input().split()))[2:]:
        M[i+1].append(j)

D=BFS(1)
for i in range(1,n+1):
    print(i,D[i]) from collections import deque def BFS(num):
    color=["white" for _ in range(n+1)]
    D=[-1 for _ in range(n+1)]

    color[num]="gray"
    D[num]=0
    queue=deque([num])

    while len(queue)>0:
        u=queue.popleft()
        for i in M[u]:
            if color[i]=="white":
                color[i]="gray"
                D[i]=D[u]+1
                queue.append(i)
        color[u]="black"

    return D BFS "white" for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) "white" color=["white" for _ in range(n+1)] color ["white" for _ in range(n+1)] -1 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) -1 D=[-1 for _ in range(n+1)] D [-1 for _ in range(n+1)] color[num]="gray" color[num] color num "gray" D[num]=0 D[num] D num 0 queue=deque([num]) queue deque([num]) deque [num] num while len(queue)>0:
        u=queue.popleft()
        for i in M[u]:
            if color[i]=="white":
                color[i]="gray"
                D[i]=D[u]+1
                queue.append(i)
        color[u]="black"

     len(queue)>0 len(queue) len queue 0 u=queue.popleft() u queue.popleft() queue.popleft queue popleft for i in M[u]:
            if color[i]=="white":
                color[i]="gray"
                D[i]=D[u]+1
                queue.append(i)
         i M[u] M u if color[i]=="white":
                color[i]="gray"
                D[i]=D[u]+1
                queue.append(i)
         color[i]=="white" color[i] color i "white" color[i]="gray" color[i] color i "gray" D[i]=D[u]+1 D[i] D i D[u]+1 D[u] D u 1 queue.append(i) queue.append queue append i color[u]="black" color[u] color u "black" return D D num num n=int(input()) n int(input()) int input() input [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] M=[[] for _ in range(n+1)] M [[] for _ in range(n+1)] for i in range(n):
    for j in list(map(int,input().split()))[2:]:
        M[i+1].append(j) i range(n) range n for j in list(map(int,input().split()))[2:]:
        M[i+1].append(j) j list(map(int,input().split()))[2:] (map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split 2: 2 M[i+1].append(j) [i+1].append [i+1] M i+1 i 1 append j D=BFS(1) D BFS(1) BFS 1 for i in range(1,n+1):
    print(i,D[i]) i range(1,n+1) range 1 n+1 n 1 print(i,D[i]) print i D[i] D i M=[[] for _ in range(n+1)] [[] for _ in range(n+1)] M D=BFS(1) BFS(1) D def BFS(num):
    color=["white" for _ in range(n+1)]
    D=[-1 for _ in range(n+1)]

    color[num]="gray"
    D[num]=0
    queue=deque([num])

    while len(queue)>0:
        u=queue.popleft()
        for i in M[u]:
            if color[i]=="white":
                color[i]="gray"
                D[i]=D[u]+1
                queue.append(i)
        color[u]="black"

    return D def BFS(num):
    color=["white" for _ in range(n+1)]
    D=[-1 for _ in range(n+1)]

    color[num]="gray"
    D[num]=0
    queue=deque([num])

    while len(queue)>0:
        u=queue.popleft()
        for i in M[u]:
            if color[i]=="white":
                color[i]="gray"
                D[i]=D[u]+1
                queue.append(i)
        color[u]="black"

    return D BFS n=int(input()) int(input()) n
from collections import deque


def BFS(num):
    color=["white" for _ in range(n+1)]
    D=[-1 for _ in range(n+1)]

    color[num]="gray"
    D[num]=0
    queue=deque([num])

    while len(queue)>0:
        u=queue.popleft()
        for i in M[u]:
            if color[i]=="white":
                color[i]="gray"
                D[i]=D[u]+1
                queue.append(i)
        color[u]="black"

    return D


n=int(input())

M=[[] for _ in range(n+1)]
for i in range(n):
    for j in list(map(int,input().split()))[2:]:
        M[i+1].append(j)

D=BFS(1)
for i in range(1,n+1):
    print(i,D[i])
