from collections import deque

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

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

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

while queue:
    i, d = queue.popleft()
    dist[i] = d
    for c in g[i]:
        if not visited[c]:
            visited[c] = 1
            queue.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 queue = deque() queue deque() deque while n:
    l = list(map(int, input().split()))
    connected = [i - 1 for i in l[2:]]
    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 connected = [i - 1 for i in l[2:]] connected [i - 1 for i in l[2:]] 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 queue.append((0, 0)) queue.append queue append (0, 0) 0 0 visited[0] = 1 visited[0] visited 0 1 while queue:
    i, d = queue.popleft()
    dist[i] = d
    for c in g[i]:
        if not visited[c]:
            visited[c] = 1
            queue.append((c, d + 1)) queue i, d = queue.popleft() i queue.popleft() queue.popleft queue popleft d dist[i] = d dist[i] dist i d for c in g[i]:
        if not visited[c]:
            visited[c] = 1
            queue.append((c, d + 1)) c g[i] g i if not visited[c]:
            visited[c] = 1
            queue.append((c, d + 1)) not visited[c] visited[c] visited c visited[c] = 1 visited[c] visited c 1 queue.append((c, d + 1)) queue.append queue 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 n = int(input()) int(input()) n g = [None] * n [None] * n g n -= 1 1 n d = queue.popleft() queue.popleft() d l = list(map(int, input().split())) list(map(int, input().split())) l queue = deque() deque() queue i, d = queue.popleft() queue.popleft() i connected = [i - 1 for i in l[2:]] [i - 1 for i in l[2:]] connected dist = [-1] * n [-1] * n dist visited = [0] * n [0] * n visited
from collections import deque

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

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

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

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

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