# -*- coding: utf-8 -*-

import sys
from collections import deque

sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
INF=float('inf')

N=INT()
nodes=[[] for i in range(N)]
for i in range(N):
    l=LIST()
    u=l[0]
    l=l[2:]
    for v in l:
        nodes[u-1].append(v-1)
    nodes[u-1].sort()

que=deque()
que.append((0, 0))
costs=[-1]*N
while len(que):
    cost,node=que.popleft()
    if costs[node]==-1:
        costs[node]=cost
        for v in nodes[node]:
            que.append((cost+1, v))

for i in range(N):
    print(i+1, costs[i])
 import sys sys from collections import deque sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 def input(): return sys.stdin.readline().strip() input def INT(): return int(input()) INT def MAP(): return map(int, input().split()) MAP def LIST(): return list(map(int, input().split())) LIST INF=float('inf') INF float('inf') float 'inf' N=INT() N INT() INT [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] nodes=[[] for i in range(N)] nodes [[] for i in range(N)] for i in range(N):
    l=LIST()
    u=l[0]
    l=l[2:]
    for v in l:
        nodes[u-1].append(v-1)
    nodes[u-1].sort() i range(N) range N l=LIST() l LIST() LIST u=l[0] u l[0] l 0 l=l[2:] l l[2:] l 2: 2 for v in l:
        nodes[u-1].append(v-1)
     v l nodes[u-1].append(v-1) [u-1].append [u-1] nodes u-1 u 1 append v-1 v 1 nodes[u-1].sort() [u-1].sort [u-1] nodes u-1 u 1 sort que=deque() que deque() deque que.append((0, 0)) que.append que append (0, 0) 0 0 costs=[-1]*N costs [-1]*N [-1] -1 N while len(que):
    cost,node=que.popleft()
    if costs[node]==-1:
        costs[node]=cost
        for v in nodes[node]:
            que.append((cost+1, v)) len(que) len que cost,node=que.popleft() cost que.popleft() que.popleft que popleft node if costs[node]==-1:
        costs[node]=cost
        for v in nodes[node]:
            que.append((cost+1, v)) costs[node]==-1 costs[node] costs node -1 costs[node]=cost costs[node] costs node cost for v in nodes[node]:
            que.append((cost+1, v)) v nodes[node] nodes node que.append((cost+1, v)) que.append que append (cost+1, v) cost+1 cost 1 v for i in range(N):
    print(i+1, costs[i]) i range(N) range N print(i+1, costs[i]) print i+1 i 1 costs[i] costs i def LIST(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) LIST N=INT() INT() N u=l[0] l[0] u INF=float('inf') float('inf') INF l=l[2:] l[2:] l def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input cost,node=que.popleft() que.popleft() cost que=deque() deque() que node=que.popleft() que.popleft() node costs=[-1]*N [-1]*N costs nodes=[[] for i in range(N)] [[] for i in range(N)] nodes def INT(): return int(input()) def INT(): return int(input()) INT def MAP(): return map(int, input().split()) def MAP(): return map(int, input().split()) MAP l=LIST() LIST() l
# -*- coding: utf-8 -*-

import sys
from collections import deque

sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
INF=float('inf')

N=INT()
nodes=[[] for i in range(N)]
for i in range(N):
    l=LIST()
    u=l[0]
    l=l[2:]
    for v in l:
        nodes[u-1].append(v-1)
    nodes[u-1].sort()

que=deque()
que.append((0, 0))
costs=[-1]*N
while len(que):
    cost,node=que.popleft()
    if costs[node]==-1:
        costs[node]=cost
        for v in nodes[node]:
            que.append((cost+1, v))

for i in range(N):
    print(i+1, costs[i])

