import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

n = int(input())
dis = [-1] * n
info = [list(map(int, input().split())) for _ in range(n)]

stack = [[0, 0]]
ans = 0
while len(stack) > 0:
    num = stack.pop(0)
    if dis[num[0]] == -1:
        dis[num[0]] = num[1]
    for i in info[num[0]][2:]:
        if dis[i-1] == -1:
            stack.append([i - 1, num[1] + 1])
for i in range(n):
    print(str(i+1) + " " + str(dis[i]))
 import sys sys import math math import string string import fractions fractions import random random from operator import itemgetter import itertools itertools from collections import deque import copy copy import heapq heapq import bisect bisect MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 INF = float('inf') INF float('inf') float 'inf' input = lambda: sys.stdin.readline().strip() input lambda: sys.stdin.readline().strip() sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip sys.setrecursionlimit(10 ** 8) sys.setrecursionlimit sys setrecursionlimit 10 ** 8 10 8 n = int(input()) n int(input()) int input() input dis = [-1] * n dis [-1] * n [-1] -1 n list(map(int, input().split())) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split info = [list(map(int, input().split())) for _ in range(n)] info [list(map(int, input().split())) for _ in range(n)] stack = [[0, 0]] stack [[0, 0]] [0, 0] 0 0 ans = 0 ans 0 while len(stack) > 0:
    num = stack.pop(0)
    if dis[num[0]] == -1:
        dis[num[0]] = num[1]
    for i in info[num[0]][2:]:
        if dis[i-1] == -1:
            stack.append([i - 1, num[1] + 1]) len(stack) > 0 len(stack) len stack 0 num = stack.pop(0) num stack.pop(0) stack.pop stack pop 0 if dis[num[0]] == -1:
        dis[num[0]] = num[1]
     dis[num[0]] == -1 dis[num[0]] dis num[0] num 0 -1 dis[num[0]] = num[1] dis[num[0]] dis num[0] num 0 num[1] num 1 for i in info[num[0]][2:]:
        if dis[i-1] == -1:
            stack.append([i - 1, num[1] + 1]) i info[num[0]][2:] [num[0]] info num[0] num 0 2: 2 if dis[i-1] == -1:
            stack.append([i - 1, num[1] + 1]) dis[i-1] == -1 dis[i-1] dis i-1 i 1 -1 stack.append([i - 1, num[1] + 1]) stack.append stack append [i - 1, num[1] + 1] i - 1 i 1 num[1] + 1 num[1] num 1 1 for i in range(n):
    print(str(i+1) + " " + str(dis[i])) i range(n) range n print(str(i+1) + " " + str(dis[i])) print str(i+1) + " " + str(dis[i]) str(i+1) + " " str(i+1) str i+1 i 1 " " str(dis[i]) str dis[i] dis i n = int(input()) int(input()) n num = stack.pop(0) stack.pop(0) num info = [list(map(int, input().split())) for _ in range(n)] [list(map(int, input().split())) for _ in range(n)] info MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD stack = [[0, 0]] [[0, 0]] stack ans = 0 0 ans input = lambda: sys.stdin.readline().strip() lambda: sys.stdin.readline().strip() input dis = [-1] * n [-1] * n dis INF = float('inf') float('inf') INF
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

n = int(input())
dis = [-1] * n
info = [list(map(int, input().split())) for _ in range(n)]

stack = [[0, 0]]
ans = 0
while len(stack) > 0:
    num = stack.pop(0)
    if dis[num[0]] == -1:
        dis[num[0]] = num[1]
    for i in info[num[0]][2:]:
        if dis[i-1] == -1:
            stack.append([i - 1, num[1] + 1])
for i in range(n):
    print(str(i+1) + " " + str(dis[i]))

