import queue
from collections import namedtuple
T = namedtuple("T", "u d")

n = int(input())
path = dict()
ans = [-1] * (n + 1)

for i in range(n):
    a = [int(x) for x in input().split()]
    a.reverse()

    u, k = a.pop(), a.pop()
    path[u] = set()
    for j in range(k):
        path[u].add(a.pop())


def bfs(s):
    q = queue.Queue()
    q.put(s)

    while q.qsize() != 0:
        p = q.get()
        if p.d < ans[p.u] or ans[p.u] < 0:
            ans[p.u] = p.d
            for r in path[p.u]:
                q.put(T(r, p.d + 1))


bfs(T(1, 0))
for i in range(1, n + 1):
    print(i, ans[i])
 import queue queue from collections import namedtuple T = namedtuple("T", "u d") T namedtuple("T", "u d") namedtuple "T" "u d" n = int(input()) n int(input()) int input() input path = dict() path dict() dict ans = [-1] * (n + 1) ans [-1] * (n + 1) [-1] -1 n + 1 n 1 for i in range(n):
    a = [int(x) for x in input().split()]
    a.reverse()

    u, k = a.pop(), a.pop()
    path[u] = set()
    for j in range(k):
        path[u].add(a.pop()) i range(n) range n int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x a = [int(x) for x in input().split()] a [int(x) for x in input().split()] a.reverse() a.reverse a reverse u, k = a.pop(), a.pop() u a.pop() a.pop a pop k a.pop() a.pop a pop path[u] = set() path[u] path u set() set for j in range(k):
        path[u].add(a.pop()) j range(k) range k path[u].add(a.pop()) [u].add [u] path u add a.pop() a.pop a pop def bfs(s):
    q = queue.Queue()
    q.put(s)

    while q.qsize() != 0:
        p = q.get()
        if p.d < ans[p.u] or ans[p.u] < 0:
            ans[p.u] = p.d
            for r in path[p.u]:
                q.put(T(r, p.d + 1)) bfs q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(s) q.put q put s while q.qsize() != 0:
        p = q.get()
        if p.d < ans[p.u] or ans[p.u] < 0:
            ans[p.u] = p.d
            for r in path[p.u]:
                q.put(T(r, p.d + 1)) q.qsize() != 0 q.qsize() q.qsize q qsize 0 p = q.get() p q.get() q.get q get if p.d < ans[p.u] or ans[p.u] < 0:
            ans[p.u] = p.d
            for r in path[p.u]:
                q.put(T(r, p.d + 1)) p.d < ans[p.u] or ans[p.u] < 0 p.d < ans[p.u] p.d p d ans[p.u] ans p.u p u ans[p.u] < 0 ans[p.u] ans p.u p u 0 ans[p.u] = p.d ans[p.u] ans p.u p u p.d p d for r in path[p.u]:
                q.put(T(r, p.d + 1)) r path[p.u] path p.u p u q.put(T(r, p.d + 1)) q.put q put T(r, p.d + 1) T r p.d + 1 p.d p d 1 s s bfs(T(1, 0)) bfs T(1, 0) T 1 0 for i in range(1, n + 1):
    print(i, ans[i]) i range(1, n + 1) range 1 n + 1 n 1 print(i, ans[i]) print i ans[i] ans i path = dict() dict() path T = namedtuple("T", "u d") namedtuple("T", "u d") T a = [int(x) for x in input().split()] [int(x) for x in input().split()] a def bfs(s):
    q = queue.Queue()
    q.put(s)

    while q.qsize() != 0:
        p = q.get()
        if p.d < ans[p.u] or ans[p.u] < 0:
            ans[p.u] = p.d
            for r in path[p.u]:
                q.put(T(r, p.d + 1)) def bfs(s):
    q = queue.Queue()
    q.put(s)

    while q.qsize() != 0:
        p = q.get()
        if p.d < ans[p.u] or ans[p.u] < 0:
            ans[p.u] = p.d
            for r in path[p.u]:
                q.put(T(r, p.d + 1)) bfs u, k = a.pop(), a.pop() a.pop() u n = int(input()) int(input()) n k = a.pop(), a.pop() a.pop() k ans = [-1] * (n + 1) [-1] * (n + 1) ans
import queue
from collections import namedtuple
T = namedtuple("T", "u d")

n = int(input())
path = dict()
ans = [-1] * (n + 1)

for i in range(n):
    a = [int(x) for x in input().split()]
    a.reverse()

    u, k = a.pop(), a.pop()
    path[u] = set()
    for j in range(k):
        path[u].add(a.pop())


def bfs(s):
    q = queue.Queue()
    q.put(s)

    while q.qsize() != 0:
        p = q.get()
        if p.d < ans[p.u] or ans[p.u] < 0:
            ans[p.u] = p.d
            for r in path[p.u]:
                q.put(T(r, p.d + 1))


bfs(T(1, 0))
for i in range(1, n + 1):
    print(i, ans[i])

