# https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/11/ALDS1_11_C

from collections import deque

n = int(input())
graph = {}
res = [-1 for i in range(n + 1)]

for i in range(1, n + 1):
    data = input().split()
    graph[i] = [int(i) for i in data[2:]]

visited = set()
q = deque()
q.append((1, 0))  # node,distance
visited.add(1)

while q:
    node, dis = q.popleft()
    res[node] = dis
    for _next in graph[node]:
        if _next not in visited:
            visited.add(_next)
            q.append((_next, dis + 1))

for i in range(1, n + 1):
    print(i, res[i])
 from collections import deque n = int(input()) n int(input()) int input() input graph = {} graph {} -1 for i in range(n + 1) for i in range(n + 1) i range(n + 1) range n + 1 n 1 for i in range(n + 1) -1 res = [-1 for i in range(n + 1)] res [-1 for i in range(n + 1)] for i in range(1, n + 1):
    data = input().split()
    graph[i] = [int(i) for i in data[2:]] i range(1, n + 1) range 1 n + 1 n 1 data = input().split() data input().split() ().split () input split int(i) for i in data[2:] for i in data[2:] i data[2:] data 2: 2 for i in data[2:] int(i) int i graph[i] = [int(i) for i in data[2:]] graph[i] graph i [int(i) for i in data[2:]] visited = set() visited set() set q = deque() q deque() deque q.append((1, 0)) q.append q append (1, 0) 1 0 visited.add(1) visited.add visited add 1 while q:
    node, dis = q.popleft()
    res[node] = dis
    for _next in graph[node]:
        if _next not in visited:
            visited.add(_next)
            q.append((_next, dis + 1)) q node, dis = q.popleft() node q.popleft() q.popleft q popleft dis res[node] = dis res[node] res node dis for _next in graph[node]:
        if _next not in visited:
            visited.add(_next)
            q.append((_next, dis + 1)) _next graph[node] graph node if _next not in visited:
            visited.add(_next)
            q.append((_next, dis + 1)) _next not in visited _next visited visited.add(_next) visited.add visited add _next q.append((_next, dis + 1)) q.append q append (_next, dis + 1) _next dis + 1 dis 1 for i in range(1, n + 1):
    print(i, res[i]) i range(1, n + 1) range 1 n + 1 n 1 print(i, res[i]) print i res[i] res i visited = set() set() visited q = deque() deque() q node, dis = q.popleft() q.popleft() node n = int(input()) int(input()) n graph = {} {} graph data = input().split() input().split() data dis = q.popleft() q.popleft() dis res = [-1 for i in range(n + 1)] [-1 for i in range(n + 1)] res
# https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/11/ALDS1_11_C

from collections import deque

n = int(input())
graph = {}
res = [-1 for i in range(n + 1)]

for i in range(1, n + 1):
    data = input().split()
    graph[i] = [int(i) for i in data[2:]]

visited = set()
q = deque()
q.append((1, 0))  # node,distance
visited.add(1)

while q:
    node, dis = q.popleft()
    res[node] = dis
    for _next in graph[node]:
        if _next not in visited:
            visited.add(_next)
            q.append((_next, dis + 1))

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

