from collections import deque
from typing import List
import sys


def bfs(adj_list: List[List[int]]) -> List[int]:
    n = len(adj_list)
    result: List[int] = [sys.maxsize for _ in range(n)]
    result[0] = 0
    queue: deque = deque()
    queue.append(0)

    while len(queue) > 0:
        cur = queue.popleft()
        for vertex in adj_list[cur]:
            if result[cur] + 1 < result[vertex]:
                result[vertex] = result[cur] + 1
                queue.append(vertex)

    return result


if __name__ == "__main__":
    num_vertices = int(input())
    adj_list: List[List[int]] = [[] for _ in range(num_vertices)]

    for _ in range(num_vertices):
        vertex, num_adjs, *adjs = map(lambda x: int(x), input().split())
        for i in range(num_adjs):
            adj_list[vertex - 1].append(adjs[i] - 1)

    result = bfs(adj_list)

    for i in range(num_vertices):
        if sys.maxsize == result[i]:
            result[i] = -1
        print(f"{i + 1} {result[i]}")
 from collections import deque from typing import List import sys sys def bfs(adj_list: List[List[int]]) -> List[int]:
    n = len(adj_list)
    result: List[int] = [sys.maxsize for _ in range(n)]
    result[0] = 0
    queue: deque = deque()
    queue.append(0)

    while len(queue) > 0:
        cur = queue.popleft()
        for vertex in adj_list[cur]:
            if result[cur] + 1 < result[vertex]:
                result[vertex] = result[cur] + 1
                queue.append(vertex)

    return result bfs n = len(adj_list) n len(adj_list) len adj_list sys.maxsize for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) sys.maxsize sys maxsize result: List[int] = [sys.maxsize for _ in range(n)] result [sys.maxsize for _ in range(n)] result[0] = 0 result[0] result 0 0 queue: deque = deque() queue deque() deque queue.append(0) queue.append queue append 0 while len(queue) > 0:
        cur = queue.popleft()
        for vertex in adj_list[cur]:
            if result[cur] + 1 < result[vertex]:
                result[vertex] = result[cur] + 1
                queue.append(vertex)

     len(queue) > 0 len(queue) len queue 0 cur = queue.popleft() cur queue.popleft() queue.popleft queue popleft for vertex in adj_list[cur]:
            if result[cur] + 1 < result[vertex]:
                result[vertex] = result[cur] + 1
                queue.append(vertex)

     vertex adj_list[cur] adj_list cur if result[cur] + 1 < result[vertex]:
                result[vertex] = result[cur] + 1
                queue.append(vertex)

     result[cur] + 1 < result[vertex] result[cur] + 1 result[cur] result cur 1 result[vertex] result vertex result[vertex] = result[cur] + 1 result[vertex] result vertex result[cur] + 1 result[cur] result cur 1 queue.append(vertex) queue.append queue append vertex return result result adj_list: List[List[int]] adj_list if __name__ == "__main__":
    num_vertices = int(input())
    adj_list: List[List[int]] = [[] for _ in range(num_vertices)]

    for _ in range(num_vertices):
        vertex, num_adjs, *adjs = map(lambda x: int(x), input().split())
        for i in range(num_adjs):
            adj_list[vertex - 1].append(adjs[i] - 1)

    result = bfs(adj_list)

    for i in range(num_vertices):
        if sys.maxsize == result[i]:
            result[i] = -1
        print(f"{i + 1} {result[i]}") __name__ == "__main__" __name__ "__main__" num_vertices = int(input()) num_vertices int(input()) int input() input [] for _ in range(num_vertices) for _ in range(num_vertices) _ range(num_vertices) range num_vertices for _ in range(num_vertices) [] adj_list: List[List[int]] = [[] for _ in range(num_vertices)] adj_list [[] for _ in range(num_vertices)] for _ in range(num_vertices):
        vertex, num_adjs, *adjs = map(lambda x: int(x), input().split())
        for i in range(num_adjs):
            adj_list[vertex - 1].append(adjs[i] - 1)

     _ range(num_vertices) range num_vertices vertex, num_adjs, *adjs = map(lambda x: int(x), input().split()) vertex map(lambda x: int(x), input().split()) map lambda x: int(x) int(x) int x x input().split() ().split () input split num_adjs *adjs adjs for i in range(num_adjs):
            adj_list[vertex - 1].append(adjs[i] - 1)

     i range(num_adjs) range num_adjs adj_list[vertex - 1].append(adjs[i] - 1) [vertex - 1].append [vertex - 1] adj_list vertex - 1 vertex 1 append adjs[i] - 1 adjs[i] adjs i 1 result = bfs(adj_list) result bfs(adj_list) bfs adj_list for i in range(num_vertices):
        if sys.maxsize == result[i]:
            result[i] = -1
        print(f"{i + 1} {result[i]}") i range(num_vertices) range num_vertices if sys.maxsize == result[i]:
            result[i] = -1
         sys.maxsize == result[i] sys.maxsize sys maxsize result[i] result i result[i] = -1 result[i] result i -1 print(f"{i + 1} {result[i]}") print f"{i + 1} {result[i]}" adj_list: List[List[int]] = [[] for _ in range(num_vertices)] [[] for _ in range(num_vertices)] adj_list num_vertices = int(input()) int(input()) num_vertices result = bfs(adj_list) bfs(adj_list) result def bfs(adj_list: List[List[int]]) -> List[int]:
    n = len(adj_list)
    result: List[int] = [sys.maxsize for _ in range(n)]
    result[0] = 0
    queue: deque = deque()
    queue.append(0)

    while len(queue) > 0:
        cur = queue.popleft()
        for vertex in adj_list[cur]:
            if result[cur] + 1 < result[vertex]:
                result[vertex] = result[cur] + 1
                queue.append(vertex)

    return result def bfs(adj_list: List[List[int]]) -> List[int]:
    n = len(adj_list)
    result: List[int] = [sys.maxsize for _ in range(n)]
    result[0] = 0
    queue: deque = deque()
    queue.append(0)

    while len(queue) > 0:
        cur = queue.popleft()
        for vertex in adj_list[cur]:
            if result[cur] + 1 < result[vertex]:
                result[vertex] = result[cur] + 1
                queue.append(vertex)

    return result bfs vertex, num_adjs, *adjs = map(lambda x: int(x), input().split()) map(lambda x: int(x), input().split()) vertex num_adjs, *adjs = map(lambda x: int(x), input().split()) map(lambda x: int(x), input().split()) num_adjs
from collections import deque
from typing import List
import sys


def bfs(adj_list: List[List[int]]) -> List[int]:
    n = len(adj_list)
    result: List[int] = [sys.maxsize for _ in range(n)]
    result[0] = 0
    queue: deque = deque()
    queue.append(0)

    while len(queue) > 0:
        cur = queue.popleft()
        for vertex in adj_list[cur]:
            if result[cur] + 1 < result[vertex]:
                result[vertex] = result[cur] + 1
                queue.append(vertex)

    return result


if __name__ == "__main__":
    num_vertices = int(input())
    adj_list: List[List[int]] = [[] for _ in range(num_vertices)]

    for _ in range(num_vertices):
        vertex, num_adjs, *adjs = map(lambda x: int(x), input().split())
        for i in range(num_adjs):
            adj_list[vertex - 1].append(adjs[i] - 1)

    result = bfs(adj_list)

    for i in range(num_vertices):
        if sys.maxsize == result[i]:
            result[i] = -1
        print(f"{i + 1} {result[i]}")

