import queue

n = int(input())

Graph = [[x-1 for x in list(map(int, input().split()))[2:]] for i in range(n)]


# for i in range(n):
#     li = list(map(int, input().split()))
#     a = li[0]
#     a-=1
#     b = li[2:]
#     Graph[a].append(b)


dist = [-1]*n
q = queue.Queue()

dist[0] = 0
q.put(0)

while not q.empty():
    v = q.get()

    for nv in Graph[v]:
        if dist[nv] != -1: continue

        dist[nv] = dist[v] + 1
        q.put(nv)


for v in range(n):
    print(v+1, dist[v]) import queue queue n = int(input()) n int(input()) int input() input [x-1 for x in list(map(int, input().split()))[2:]] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [x-1 for x in list(map(int, input().split()))[2:]] Graph = [[x-1 for x in list(map(int, input().split()))[2:]] for i in range(n)] Graph [[x-1 for x in list(map(int, input().split()))[2:]] for i in range(n)] dist = [-1]*n dist [-1]*n [-1] -1 n q = queue.Queue() q queue.Queue() queue.Queue queue Queue dist[0] = 0 dist[0] dist 0 0 q.put(0) q.put q put 0 while not q.empty():
    v = q.get()

    for nv in Graph[v]:
        if dist[nv] != -1: continue

        dist[nv] = dist[v] + 1
        q.put(nv) not q.empty() q.empty() q.empty q empty v = q.get() v q.get() q.get q get for nv in Graph[v]:
        if dist[nv] != -1: continue

        dist[nv] = dist[v] + 1
        q.put(nv) nv Graph[v] Graph v if dist[nv] != -1: continue

         dist[nv] != -1 dist[nv] dist nv -1 dist[nv] = dist[v] + 1 dist[nv] dist nv dist[v] + 1 dist[v] dist v 1 q.put(nv) q.put q put nv for v in range(n):
    print(v+1, dist[v]) v range(n) range n print(v+1, dist[v]) print v+1 v 1 dist[v] dist v Graph = [[x-1 for x in list(map(int, input().split()))[2:]] for i in range(n)] [[x-1 for x in list(map(int, input().split()))[2:]] for i in range(n)] Graph v = q.get() q.get() v dist = [-1]*n [-1]*n dist n = int(input()) int(input()) n q = queue.Queue() queue.Queue() q
import queue

n = int(input())

Graph = [[x-1 for x in list(map(int, input().split()))[2:]] for i in range(n)]


# for i in range(n):
#     li = list(map(int, input().split()))
#     a = li[0]
#     a-=1
#     b = li[2:]
#     Graph[a].append(b)


dist = [-1]*n
q = queue.Queue()

dist[0] = 0
q.put(0)

while not q.empty():
    v = q.get()

    for nv in Graph[v]:
        if dist[nv] != -1: continue

        dist[nv] = dist[v] + 1
        q.put(nv)


for v in range(n):
    print(v+1, dist[v])
