from collections import deque

n = int(input())
g = [None] * n
visited = [0] * n
dist = [-1] * n
Q = deque()

while n:
    l = list(map(int, input().split()))
    g[l[0]-1] = [i-1 for i in l[2:]]
    n -= 1

Q.append((0, 0)) 
visited[0] = 1

while Q:
    i, d = Q.popleft()         
    dist[i] = d
    for c in g[i]:
        if not visited[c]:
            visited[c] = 1
            Q.append((c, d+1))

for i, d in enumerate(dist):
    print(i+1, d) from collections import deque n = int(input()) n int(input()) int input() input g = [None] * n g [None] * n [None] None n visited = [0] * n visited [0] * n [0] 0 n dist = [-1] * n dist [-1] * n [-1] -1 n Q = deque() Q deque() deque while n:
    l = list(map(int, input().split()))
    g[l[0]-1] = [i-1 for i in l[2:]]
    n -= 1 n l = list(map(int, input().split())) l list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split i-1 for i in l[2:] for i in l[2:] i l[2:] l 2: 2 for i in l[2:] i-1 i 1 g[l[0]-1] = [i-1 for i in l[2:]] g[l[0]-1] g l[0]-1 l[0] l 0 1 [i-1 for i in l[2:]] n -= 1 n 1 Q.append((0, 0)) Q.append Q append (0, 0) 0 0 visited[0] = 1 visited[0] visited 0 1 while Q:
    i, d = Q.popleft()         
    dist[i] = d
    for c in g[i]:
        if not visited[c]:
            visited[c] = 1
            Q.append((c, d+1)) Q i, d = Q.popleft() i Q.popleft() Q.popleft Q popleft d dist[i] = d dist[i] dist i d for c in g[i]:
        if not visited[c]:
            visited[c] = 1
            Q.append((c, d+1)) c g[i] g i if not visited[c]:
            visited[c] = 1
            Q.append((c, d+1)) not visited[c] visited[c] visited c visited[c] = 1 visited[c] visited c 1 Q.append((c, d+1)) Q.append Q append (c, d+1) c d+1 d 1 for i, d in enumerate(dist):
    print(i+1, d) i d enumerate(dist) enumerate dist print(i+1, d) print i+1 i 1 d g = [None] * n [None] * n g dist = [-1] * n [-1] * n dist d = Q.popleft() Q.popleft() d i, d = Q.popleft() Q.popleft() i n -= 1 1 n visited = [0] * n [0] * n visited l = list(map(int, input().split())) list(map(int, input().split())) l n = int(input()) int(input()) n Q = deque() deque() Q
from collections import deque

n = int(input())
g = [None] * n
visited = [0] * n
dist = [-1] * n
Q = deque()

while n:
    l = list(map(int, input().split()))
    g[l[0]-1] = [i-1 for i in l[2:]]
    n -= 1

Q.append((0, 0)) 
visited[0] = 1

while Q:
    i, d = Q.popleft()         
    dist[i] = d
    for c in g[i]:
        if not visited[c]:
            visited[c] = 1
            Q.append((c, d+1))

for i, d in enumerate(dist):
    print(i+1, d)
