import sys
input = sys.stdin.readline

from collections import deque

def getNeighbor(A, i):
    a = len(A)
    for k in range(2, a):
        M[i][A[k]-1] = 1

def bfs():
    u = q.popleft()
    c[u] = 1

    for v in range(n):
        if M[u][v] and c[v] == 0:
            q.append(v)
            d[v] = d[u] + 1
            c[v] = 1

    if q:
        bfs()

    return

    bfs()

q = deque([0])

n = int(input())
#color
c = [0] * n
d = [0] + [-1] * (n-1) #depth

M = [[0] * n for _ in range(n)]
t = 0

for i in range(n):
    A = list(map(int, input().split()))
    getNeighbor(A,i)

bfs()

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

 import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline from collections import deque def getNeighbor(A, i):
    a = len(A)
    for k in range(2, a):
        M[i][A[k]-1] = 1 getNeighbor a = len(A) a len(A) len A for k in range(2, a):
        M[i][A[k]-1] = 1 k range(2, a) range 2 a M[i][A[k]-1] = 1 M[i][A[k]-1] [i] M i A[k]-1 A[k] A k 1 1 A A i i def bfs():
    u = q.popleft()
    c[u] = 1

    for v in range(n):
        if M[u][v] and c[v] == 0:
            q.append(v)
            d[v] = d[u] + 1
            c[v] = 1

    if q:
        bfs()

    return

    bfs() bfs u = q.popleft() u q.popleft() q.popleft q popleft c[u] = 1 c[u] c u 1 for v in range(n):
        if M[u][v] and c[v] == 0:
            q.append(v)
            d[v] = d[u] + 1
            c[v] = 1

     v range(n) range n if M[u][v] and c[v] == 0:
            q.append(v)
            d[v] = d[u] + 1
            c[v] = 1

     M[u][v] and c[v] == 0 M[u][v] [u] M u v c[v] == 0 c[v] c v 0 q.append(v) q.append q append v d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 c[v] = 1 c[v] c v 1 if q:
        bfs()

     q bfs() bfs return bfs() bfs q = deque([0]) q deque([0]) deque [0] 0 n = int(input()) n int(input()) int input() input c = [0] * n c [0] * n [0] 0 n d = [0] + [-1] * (n-1) d [0] + [-1] * (n-1) [0] 0 [-1] * (n-1) [-1] -1 n-1 n 1 [0] * n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * n [0] 0 n M = [[0] * n for _ in range(n)] M [[0] * n for _ in range(n)] t = 0 t 0 for i in range(n):
    A = list(map(int, input().split()))
    getNeighbor(A,i) i range(n) range n A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split getNeighbor(A,i) getNeighbor A i bfs() bfs for i in range(n):
    print(str(i+1), str(d[i])) i range(n) range n print(str(i+1), str(d[i])) print str(i+1) str i+1 i 1 str(d[i]) str d[i] d i def bfs():
    u = q.popleft()
    c[u] = 1

    for v in range(n):
        if M[u][v] and c[v] == 0:
            q.append(v)
            d[v] = d[u] + 1
            c[v] = 1

    if q:
        bfs()

    return

    bfs() def bfs():
    u = q.popleft()
    c[u] = 1

    for v in range(n):
        if M[u][v] and c[v] == 0:
            q.append(v)
            d[v] = d[u] + 1
            c[v] = 1

    if q:
        bfs()

    return

    bfs() bfs A = list(map(int, input().split())) list(map(int, input().split())) A q = deque([0]) deque([0]) q n = int(input()) int(input()) n def getNeighbor(A, i):
    a = len(A)
    for k in range(2, a):
        M[i][A[k]-1] = 1 def getNeighbor(A, i):
    a = len(A)
    for k in range(2, a):
        M[i][A[k]-1] = 1 getNeighbor d = [0] + [-1] * (n-1) [0] + [-1] * (n-1) d M = [[0] * n for _ in range(n)] [[0] * n for _ in range(n)] M input = sys.stdin.readline sys.stdin.readline input c = [0] * n [0] * n c t = 0 0 t
import sys
input = sys.stdin.readline

from collections import deque

def getNeighbor(A, i):
    a = len(A)
    for k in range(2, a):
        M[i][A[k]-1] = 1

def bfs():
    u = q.popleft()
    c[u] = 1

    for v in range(n):
        if M[u][v] and c[v] == 0:
            q.append(v)
            d[v] = d[u] + 1
            c[v] = 1

    if q:
        bfs()

    return

    bfs()

q = deque([0])

n = int(input())
#color
c = [0] * n
d = [0] + [-1] * (n-1) #depth

M = [[0] * n for _ in range(n)]
t = 0

for i in range(n):
    A = list(map(int, input().split()))
    getNeighbor(A,i)

bfs()

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


