
import queue

WHITE = 0
GRAY = 1
BLACK = 2

NIL = -1
INF = 1000000000


def bfs(u):
    global Q
    Q.put(u)
    for i in range(n):
        d[i] = NIL
    d[u] = 0
    while not Q.empty():
        u = Q.get()
        for v in range(n):
            if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v)
n = int(input())

m = [[0 for i in range(n + 1)] for j in range(n + 1)]
vid = [0] * n
d = [0] * n
f = [0] * n
Q = queue.Queue()
color = [WHITE] * n
time = 0
nt = [0] * n

tmp = []
for i in range(n):
    nums=list(map(int,input().split()))
    tmp.append(nums)
    vid[i] = nums[0]

for i in range(n):
    for j in range(tmp[i][1]):
        m[i][vid.index(tmp[i][j + 2])] = 1

bfs(0)

for i in range(n):
    print(vid[i], d[i])



 import queue queue WHITE = 0 WHITE 0 GRAY = 1 GRAY 1 BLACK = 2 BLACK 2 NIL = -1 NIL -1 INF = 1000000000 INF 1000000000 def bfs(u):
    global Q
    Q.put(u)
    for i in range(n):
        d[i] = NIL
    d[u] = 0
    while not Q.empty():
        u = Q.get()
        for v in range(n):
            if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v) bfs global Q Q.put(u) Q.put Q put u for i in range(n):
        d[i] = NIL
     i range(n) range n d[i] = NIL d[i] d i NIL d[u] = 0 d[u] d u 0 while not Q.empty():
        u = Q.get()
        for v in range(n):
            if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v) not Q.empty() Q.empty() Q.empty Q empty u = Q.get() u Q.get() Q.get Q get for v in range(n):
            if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v) v range(n) range n if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v) (m[u][v] == 1) and (d[v] == NIL) m[u][v] == 1 m[u][v] [u] m u v 1 d[v] == NIL d[v] d v NIL d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 Q.put(v) Q.put Q put v u u n = int(input()) n int(input()) int input() input [0 for i in range(n + 1)] for j in range(n + 1) for j in range(n + 1) j range(n + 1) range n + 1 n 1 for j in range(n + 1) [0 for i in range(n + 1)] m = [[0 for i in range(n + 1)] for j in range(n + 1)] m [[0 for i in range(n + 1)] for j in range(n + 1)] vid = [0] * n vid [0] * n [0] 0 n d = [0] * n d [0] * n [0] 0 n f = [0] * n f [0] * n [0] 0 n Q = queue.Queue() Q queue.Queue() queue.Queue queue Queue color = [WHITE] * n color [WHITE] * n [WHITE] WHITE n time = 0 time 0 nt = [0] * n nt [0] * n [0] 0 n tmp = [] tmp [] for i in range(n):
    nums=list(map(int,input().split()))
    tmp.append(nums)
    vid[i] = nums[0] i range(n) range n nums=list(map(int,input().split())) nums list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split tmp.append(nums) tmp.append tmp append nums vid[i] = nums[0] vid[i] vid i nums[0] nums 0 for i in range(n):
    for j in range(tmp[i][1]):
        m[i][vid.index(tmp[i][j + 2])] = 1 i range(n) range n for j in range(tmp[i][1]):
        m[i][vid.index(tmp[i][j + 2])] = 1 j range(tmp[i][1]) range tmp[i][1] [i] tmp i 1 m[i][vid.index(tmp[i][j + 2])] = 1 m[i][vid.index(tmp[i][j + 2])] [i] m i vid.index(tmp[i][j + 2]) vid.index vid index tmp[i][j + 2] [i] tmp i j + 2 j 2 1 bfs(0) bfs 0 for i in range(n):
    print(vid[i], d[i]) i range(n) range n print(vid[i], d[i]) print vid[i] vid i d[i] d i m = [[0 for i in range(n + 1)] for j in range(n + 1)] [[0 for i in range(n + 1)] for j in range(n + 1)] m color = [WHITE] * n [WHITE] * n color nums=list(map(int,input().split())) list(map(int,input().split())) nums INF = 1000000000 1000000000 INF vid = [0] * n [0] * n vid d = [0] * n [0] * n d BLACK = 2 2 BLACK Q = queue.Queue() queue.Queue() Q WHITE = 0 0 WHITE GRAY = 1 1 GRAY def bfs(u):
    global Q
    Q.put(u)
    for i in range(n):
        d[i] = NIL
    d[u] = 0
    while not Q.empty():
        u = Q.get()
        for v in range(n):
            if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v) def bfs(u):
    global Q
    Q.put(u)
    for i in range(n):
        d[i] = NIL
    d[u] = 0
    while not Q.empty():
        u = Q.get()
        for v in range(n):
            if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v) bfs n = int(input()) int(input()) n NIL = -1 -1 NIL tmp = [] [] tmp f = [0] * n [0] * n f nt = [0] * n [0] * n nt time = 0 0 time

import queue

WHITE = 0
GRAY = 1
BLACK = 2

NIL = -1
INF = 1000000000


def bfs(u):
    global Q
    Q.put(u)
    for i in range(n):
        d[i] = NIL
    d[u] = 0
    while not Q.empty():
        u = Q.get()
        for v in range(n):
            if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v)
n = int(input())

m = [[0 for i in range(n + 1)] for j in range(n + 1)]
vid = [0] * n
d = [0] * n
f = [0] * n
Q = queue.Queue()
color = [WHITE] * n
time = 0
nt = [0] * n

tmp = []
for i in range(n):
    nums=list(map(int,input().split()))
    tmp.append(nums)
    vid[i] = nums[0]

for i in range(n):
    for j in range(tmp[i][1]):
        m[i][vid.index(tmp[i][j + 2])] = 1

bfs(0)

for i in range(n):
    print(vid[i], d[i])




