INFTY = 10 ** 10

import queue

def bfs(s):
    q = queue.Queue()
    q.put(s)
    d = [INFTY] * n
    d[s] = 0
    while not q.empty():
        u = q.get()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.put(v)
    for i in range(n):
        if d[i] == INFTY:
            print(i+1, -1)
        else:
            print(i+1, d[i])

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

for i in range(n):
    nums = list(map(int, input().split()))
    u = nums[0] - 1
    k = nums[1]
    for j in range(k):
        v = nums[j+2] - 1
        M[u][v] = 1

bfs(0) INFTY = 10 ** 10 INFTY 10 ** 10 10 10 import queue queue def bfs(s):
    q = queue.Queue()
    q.put(s)
    d = [INFTY] * n
    d[s] = 0
    while not q.empty():
        u = q.get()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.put(v)
    for i in range(n):
        if d[i] == INFTY:
            print(i+1, -1)
        else:
            print(i+1, d[i]) bfs q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(s) q.put q put s d = [INFTY] * n d [INFTY] * n [INFTY] INFTY n d[s] = 0 d[s] d s 0 while not q.empty():
        u = q.get()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INFTY:
                continue
            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] == 0:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.put(v)
     v range(n) range n if M[u][v] == 0:
                continue
             M[u][v] == 0 M[u][v] [u] M u v 0 continue if d[v] != INFTY:
                continue
             d[v] != INFTY d[v] d v INFTY continue 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 for i in range(n):
        if d[i] == INFTY:
            print(i+1, -1)
        else:
            print(i+1, d[i]) i range(n) range n if d[i] == INFTY:
            print(i+1, -1)
        else:
            print(i+1, d[i]) d[i] == INFTY d[i] d i INFTY print(i+1, -1) print i+1 i 1 -1 print(i+1, d[i]) print i+1 i 1 d[i] d i s s n = int(input()) n int(input()) int input() input [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)] for i in range(n):
    nums = list(map(int, input().split()))
    u = nums[0] - 1
    k = nums[1]
    for j in range(k):
        v = nums[j+2] - 1
        M[u][v] = 1 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 u = nums[0] - 1 u nums[0] - 1 nums[0] nums 0 1 k = nums[1] k nums[1] nums 1 for j in range(k):
        v = nums[j+2] - 1
        M[u][v] = 1 j range(k) range k v = nums[j+2] - 1 v nums[j+2] - 1 nums[j+2] nums j+2 j 2 1 M[u][v] = 1 M[u][v] [u] M u v 1 bfs(0) bfs 0 nums = list(map(int, input().split())) list(map(int, input().split())) nums INFTY = 10 ** 10 10 ** 10 INFTY k = nums[1] nums[1] k u = nums[0] - 1 nums[0] - 1 u n = int(input()) int(input()) n v = nums[j+2] - 1 nums[j+2] - 1 v M = [[0] * n for _ in range(n)] [[0] * n for _ in range(n)] M def bfs(s):
    q = queue.Queue()
    q.put(s)
    d = [INFTY] * n
    d[s] = 0
    while not q.empty():
        u = q.get()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.put(v)
    for i in range(n):
        if d[i] == INFTY:
            print(i+1, -1)
        else:
            print(i+1, d[i]) def bfs(s):
    q = queue.Queue()
    q.put(s)
    d = [INFTY] * n
    d[s] = 0
    while not q.empty():
        u = q.get()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.put(v)
    for i in range(n):
        if d[i] == INFTY:
            print(i+1, -1)
        else:
            print(i+1, d[i]) bfs
INFTY = 10 ** 10

import queue

def bfs(s):
    q = queue.Queue()
    q.put(s)
    d = [INFTY] * n
    d[s] = 0
    while not q.empty():
        u = q.get()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.put(v)
    for i in range(n):
        if d[i] == INFTY:
            print(i+1, -1)
        else:
            print(i+1, d[i])

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

for i in range(n):
    nums = list(map(int, input().split()))
    u = nums[0] - 1
    k = nums[1]
    for j in range(k):
        v = nums[j+2] - 1
        M[u][v] = 1

bfs(0)
