from queue import Queue

n = int(input())
A = [[0 for i in range(n)] for j in range(n)]
d = [-1 for i in range(n)]

for i in range(n):
    u, k, *v = list(map(int, input().split()))
    for j in v:
        A[int(u)-1][int(j)-1] = 1

q = Queue()
q.put(0)
d[0] = 0

while not q.empty():
    u = q.get()
    # print('visited:', u)
    for v in range(n):
        # print(u, v)
        if A[u][v] == 1 and d[v] == -1:
            q.put(v)
            d[v] = d[u] + 1

# print('-========')
for i in range(n):
    print(i+1, d[i]) from queue import Queue n = int(input()) n int(input()) int input() input [0 for i in range(n)] for j in range(n) for j in range(n) j range(n) range n for j in range(n) [0 for i in range(n)] A = [[0 for i in range(n)] for j in range(n)] A [[0 for i in range(n)] for j in range(n)] -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 d = [-1 for i in range(n)] d [-1 for i in range(n)] for i in range(n):
    u, k, *v = list(map(int, input().split()))
    for j in v:
        A[int(u)-1][int(j)-1] = 1 i range(n) range n u, k, *v = list(map(int, input().split())) u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split k *v v for j in v:
        A[int(u)-1][int(j)-1] = 1 j v A[int(u)-1][int(j)-1] = 1 A[int(u)-1][int(j)-1] [int(u)-1] A int(u)-1 int(u) int u 1 int(j)-1 int(j) int j 1 1 q = Queue() q Queue() Queue q.put(0) q.put q put 0 d[0] = 0 d[0] d 0 0 while not q.empty():
    u = q.get()
    # print('visited:', u)
    for v in range(n):
        # print(u, v)
        if A[u][v] == 1 and d[v] == -1:
            q.put(v)
            d[v] = d[u] + 1

# print('-========') not q.empty() q.empty() q.empty q empty u = q.get() u q.get() q.get q get for v in range(n):
        # print(u, v)
        if A[u][v] == 1 and d[v] == -1:
            q.put(v)
            d[v] = d[u] + 1

# print('-========') v range(n) range n if A[u][v] == 1 and d[v] == -1:
            q.put(v)
            d[v] = d[u] + 1

# print('-========') A[u][v] == 1 and d[v] == -1 A[u][v] == 1 A[u][v] [u] A u v 1 d[v] == -1 d[v] d v -1 q.put(v) q.put q put v d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 for i in range(n):
    print(i+1, d[i]) i range(n) range n print(i+1, d[i]) print i+1 i 1 d[i] d i n = int(input()) int(input()) n k, *v = list(map(int, input().split())) list(map(int, input().split())) k A = [[0 for i in range(n)] for j in range(n)] [[0 for i in range(n)] for j in range(n)] A u = q.get() q.get() u u, k, *v = list(map(int, input().split())) list(map(int, input().split())) u d = [-1 for i in range(n)] [-1 for i in range(n)] d q = Queue() Queue() q
from queue import Queue

n = int(input())
A = [[0 for i in range(n)] for j in range(n)]
d = [-1 for i in range(n)]

for i in range(n):
    u, k, *v = list(map(int, input().split()))
    for j in v:
        A[int(u)-1][int(j)-1] = 1

q = Queue()
q.put(0)
d[0] = 0

while not q.empty():
    u = q.get()
    # print('visited:', u)
    for v in range(n):
        # print(u, v)
        if A[u][v] == 1 and d[v] == -1:
            q.put(v)
            d[v] = d[u] + 1

# print('-========')
for i in range(n):
    print(i+1, d[i])
