n = int(input())

adj_list = [[] for _ in range(n + 1)]

for i in range(n):
    tmp = list(map(int, input().split()))
    u = tmp[0]
    k = tmp[1]
    for v in range(2, 2 + k):
        adj_list[u].append(tmp[v])

d = [-1] * (n + 1)
q = []
q.append(1)
d[1] = 0

while len(q) > 0:
    u = q.pop(0)
    for v in adj_list[u]:
        if d[v] == -1:
            d[v] = d[u] + 1
            q.append(v)

for idx, d in enumerate(d):
    if idx != 0:
        print("%d %d" % (idx, d))

 n = int(input()) n int(input()) int input() input [] for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) [] adj_list = [[] for _ in range(n + 1)] adj_list [[] for _ in range(n + 1)] for i in range(n):
    tmp = list(map(int, input().split()))
    u = tmp[0]
    k = tmp[1]
    for v in range(2, 2 + k):
        adj_list[u].append(tmp[v]) i range(n) range n tmp = list(map(int, input().split())) tmp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = tmp[0] u tmp[0] tmp 0 k = tmp[1] k tmp[1] tmp 1 for v in range(2, 2 + k):
        adj_list[u].append(tmp[v]) v range(2, 2 + k) range 2 2 + k 2 k adj_list[u].append(tmp[v]) [u].append [u] adj_list u append tmp[v] tmp v d = [-1] * (n + 1) d [-1] * (n + 1) [-1] -1 n + 1 n 1 q = [] q [] q.append(1) q.append q append 1 d[1] = 0 d[1] d 1 0 while len(q) > 0:
    u = q.pop(0)
    for v in adj_list[u]:
        if d[v] == -1:
            d[v] = d[u] + 1
            q.append(v) len(q) > 0 len(q) len q 0 u = q.pop(0) u q.pop(0) q.pop q pop 0 for v in adj_list[u]:
        if d[v] == -1:
            d[v] = d[u] + 1
            q.append(v) v adj_list[u] adj_list u if d[v] == -1:
            d[v] = d[u] + 1
            q.append(v) d[v] == -1 d[v] d v -1 d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 q.append(v) q.append q append v for idx, d in enumerate(d):
    if idx != 0:
        print("%d %d" % (idx, d)) idx d enumerate(d) enumerate d if idx != 0:
        print("%d %d" % (idx, d)) idx != 0 idx 0 print("%d %d" % (idx, d)) print "%d %d" % (idx, d) "%d %d" (idx, d) idx d tmp = list(map(int, input().split())) list(map(int, input().split())) tmp n = int(input()) int(input()) n u = tmp[0] tmp[0] u k = tmp[1] tmp[1] k u = q.pop(0) q.pop(0) u adj_list = [[] for _ in range(n + 1)] [[] for _ in range(n + 1)] adj_list d = [-1] * (n + 1) [-1] * (n + 1) d q = [] [] q
n = int(input())

adj_list = [[] for _ in range(n + 1)]

for i in range(n):
    tmp = list(map(int, input().split()))
    u = tmp[0]
    k = tmp[1]
    for v in range(2, 2 + k):
        adj_list[u].append(tmp[v])

d = [-1] * (n + 1)
q = []
q.append(1)
d[1] = 0

while len(q) > 0:
    u = q.pop(0)
    for v in adj_list[u]:
        if d[v] == -1:
            d[v] = d[u] + 1
            q.append(v)

for idx, d in enumerate(d):
    if idx != 0:
        print("%d %d" % (idx, d))


