n = int(input())
E = [[]]*n

for i in range(n):
    inputs = list(map(int, input().split()))
    if inputs[1]>0:
        for e in inputs[2:]:
            E[i] = E[i] + [e-1]

visited = [-1]*n
dist = [-1]*n
dist[0] = 0

from collections import deque

d = deque()
d.append(0)
visited[0] = 1

while len(d):
    nd = d.popleft()
    #print(nd, E[nd])
    for e in E[nd]:
        #print(e)
        if visited[e]<0:
            d.append(e)
            dist[e] = dist[nd] + 1
            visited[e] = 1
    #print(d, visited, dist, len(d))
            

for i in range(n):
    print(i+1, dist[i]) n = int(input()) n int(input()) int input() input E = [[]]*n E [[]]*n [[]] [] n for i in range(n):
    inputs = list(map(int, input().split()))
    if inputs[1]>0:
        for e in inputs[2:]:
            E[i] = E[i] + [e-1] i range(n) range n inputs = list(map(int, input().split())) inputs list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split if inputs[1]>0:
        for e in inputs[2:]:
            E[i] = E[i] + [e-1] inputs[1]>0 inputs[1] inputs 1 0 for e in inputs[2:]:
            E[i] = E[i] + [e-1] e inputs[2:] inputs 2: 2 E[i] = E[i] + [e-1] E[i] E i E[i] + [e-1] E[i] E i [e-1] e-1 e 1 visited = [-1]*n visited [-1]*n [-1] -1 n dist = [-1]*n dist [-1]*n [-1] -1 n dist[0] = 0 dist[0] dist 0 0 from collections import deque d = deque() d deque() deque d.append(0) d.append d append 0 visited[0] = 1 visited[0] visited 0 1 while len(d):
    nd = d.popleft()
    #print(nd, E[nd])
    for e in E[nd]:
        #print(e)
        if visited[e]<0:
            d.append(e)
            dist[e] = dist[nd] + 1
            visited[e] = 1
    #print(d, visited, dist, len(d))
             len(d) len d nd = d.popleft() nd d.popleft() d.popleft d popleft for e in E[nd]:
        #print(e)
        if visited[e]<0:
            d.append(e)
            dist[e] = dist[nd] + 1
            visited[e] = 1
    #print(d, visited, dist, len(d))
             e E[nd] E nd if visited[e]<0:
            d.append(e)
            dist[e] = dist[nd] + 1
            visited[e] = 1
    #print(d, visited, dist, len(d))
             visited[e]<0 visited[e] visited e 0 d.append(e) d.append d append e dist[e] = dist[nd] + 1 dist[e] dist e dist[nd] + 1 dist[nd] dist nd 1 visited[e] = 1 visited[e] visited e 1 for i in range(n):
    print(i+1, dist[i]) i range(n) range n print(i+1, dist[i]) print i+1 i 1 dist[i] dist i n = int(input()) int(input()) n inputs = list(map(int, input().split())) list(map(int, input().split())) inputs nd = d.popleft() d.popleft() nd visited = [-1]*n [-1]*n visited d = deque() deque() d dist = [-1]*n [-1]*n dist E = [[]]*n [[]]*n E
n = int(input())
E = [[]]*n

for i in range(n):
    inputs = list(map(int, input().split()))
    if inputs[1]>0:
        for e in inputs[2:]:
            E[i] = E[i] + [e-1]

visited = [-1]*n
dist = [-1]*n
dist[0] = 0

from collections import deque

d = deque()
d.append(0)
visited[0] = 1

while len(d):
    nd = d.popleft()
    #print(nd, E[nd])
    for e in E[nd]:
        #print(e)
        if visited[e]<0:
            d.append(e)
            dist[e] = dist[nd] + 1
            visited[e] = 1
    #print(d, visited, dist, len(d))
            

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