import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
g = [None]*n
for i in range(n):
    g[i] = readints()
# print(g)
G = [None]*n
for i in range(n):
    G[i] = []
# print(G)
for i in range(n):
    if len(g[i]) >= 3:
        for j in range(2, len(g[i])):
            # print(g[i][j])
            G[i].append(g[i][j])
#print('G', G)
dist = [-1]*n
dist[0] = 0
d = deque()
d.append(0)
# print(d)
while(len(d) != 0):
    v = d.popleft()
    if G[v] != []:
        for nv in G[v]:
            if dist[nv-1] != -1:
                continue
            dist[nv-1] = dist[v]+1
            d.append(nv-1)
for i in range(n):
    print(i+1, dist[i])
 import math math import string string import collections collections from collections import Counter from collections import deque from decimal import Decimal import sys sys import fractions fractions def readints():
    return list(map(int, input().split())) readints return list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) nCr return math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) math.factorial(n) math.factorial math factorial n math.factorial(n-r)*math.factorial(r) math.factorial(n-r) math.factorial math factorial n-r n r math.factorial(r) math.factorial math factorial r n n r r def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen) has_duplicates2 seen = [] seen [] for item in seq:
        if not(item in seen):
            seen.append(item)
     item seq if not(item in seen):
            seen.append(item)
     not(item in seen) item in seen item seen seen.append(item) seen.append seen append item return len(seq) != len(seen) len(seq) != len(seen) len(seq) len seq len(seen) len seen seq seq def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates divisor divisor = [] divisor [] for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
     i range(1, n+1) range 1 n+1 n 1 if n % i == 0:
            divisor.append(i)
     n % i == 0 n % i n i 0 divisor.append(i) divisor.append divisor append i return divisor divisor n n dx = [-1, -1, -1, 0, 0, 1, 1, 1] dx [-1, -1, -1, 0, 0, 1, 1, 1] -1 -1 -1 0 0 1 1 1 dy = [-1, 0, 1, -1, 1, -1, 0, 1] dy [-1, 0, 1, -1, 1, -1, 0, 1] -1 0 1 -1 1 -1 0 1 n = int(input()) n int(input()) int input() input g = [None]*n g [None]*n [None] None n for i in range(n):
    g[i] = readints()
# print(g) i range(n) range n g[i] = readints() g[i] g i readints() readints G = [None]*n G [None]*n [None] None n for i in range(n):
    G[i] = []
# print(G) i range(n) range n G[i] = [] G[i] G i [] for i in range(n):
    if len(g[i]) >= 3:
        for j in range(2, len(g[i])):
            # print(g[i][j])
            G[i].append(g[i][j])
#print('G', G) i range(n) range n if len(g[i]) >= 3:
        for j in range(2, len(g[i])):
            # print(g[i][j])
            G[i].append(g[i][j])
#print('G', G) len(g[i]) >= 3 len(g[i]) len g[i] g i 3 for j in range(2, len(g[i])):
            # print(g[i][j])
            G[i].append(g[i][j])
#print('G', G) j range(2, len(g[i])) range 2 len(g[i]) len g[i] g i G[i].append(g[i][j]) [i].append [i] G i append g[i][j] [i] g i j dist = [-1]*n dist [-1]*n [-1] -1 n dist[0] = 0 dist[0] dist 0 0 d = deque() d deque() deque d.append(0) d.append d append 0 while(len(d) != 0):
    v = d.popleft()
    if G[v] != []:
        for nv in G[v]:
            if dist[nv-1] != -1:
                continue
            dist[nv-1] = dist[v]+1
            d.append(nv-1) len(d) != 0 len(d) len d 0 v = d.popleft() v d.popleft() d.popleft d popleft if G[v] != []:
        for nv in G[v]:
            if dist[nv-1] != -1:
                continue
            dist[nv-1] = dist[v]+1
            d.append(nv-1) G[v] != [] G[v] G v [] for nv in G[v]:
            if dist[nv-1] != -1:
                continue
            dist[nv-1] = dist[v]+1
            d.append(nv-1) nv G[v] G v if dist[nv-1] != -1:
                continue
             dist[nv-1] != -1 dist[nv-1] dist nv-1 nv 1 -1 continue dist[nv-1] = dist[v]+1 dist[nv-1] dist nv-1 nv 1 dist[v]+1 dist[v] dist v 1 d.append(nv-1) d.append d append nv-1 nv 1 for i in range(n):
    print(i+1, dist[i]) i range(n) range n print(i+1, dist[i]) print i+1 i 1 dist[i] dist i def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen) def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen) has_duplicates2 dy = [-1, 0, 1, -1, 1, -1, 0, 1] [-1, 0, 1, -1, 1, -1, 0, 1] dy g = [None]*n [None]*n g def readints():
    return list(map(int, input().split())) def readints():
    return list(map(int, input().split())) readints n = int(input()) int(input()) n d = deque() deque() d v = d.popleft() d.popleft() v def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) nCr def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates divisor dist = [-1]*n [-1]*n dist dx = [-1, -1, -1, 0, 0, 1, 1, 1] [-1, -1, -1, 0, 0, 1, 1, 1] dx G = [None]*n [None]*n G
import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
g = [None]*n
for i in range(n):
    g[i] = readints()
# print(g)
G = [None]*n
for i in range(n):
    G[i] = []
# print(G)
for i in range(n):
    if len(g[i]) >= 3:
        for j in range(2, len(g[i])):
            # print(g[i][j])
            G[i].append(g[i][j])
#print('G', G)
dist = [-1]*n
dist[0] = 0
d = deque()
d.append(0)
# print(d)
while(len(d) != 0):
    v = d.popleft()
    if G[v] != []:
        for nv in G[v]:
            if dist[nv-1] != -1:
                continue
            dist[nv-1] = dist[v]+1
            d.append(nv-1)
for i in range(n):
    print(i+1, dist[i])

