from collections import deque
if __name__ == "__main__":
    n = int(input())
    graph = []
    for _ in range(n):
        v, k, *tmp = map(int, input().split())
        for j in range(len(tmp)):
            tmp[j] -= 1
        graph.append(tmp)
    visited = [False] * n
    qu = deque()
    qu.appendleft((0, 0))
    ans = [-1] * n
    visited[0] = True

    while qu:
        now, dis = qu.pop()
        ans[now] = dis

        for nei in graph[now]:
            if not visited[nei]:
                visited[nei] = True
                qu.appendleft((nei, dis + 1))
    for i in range(n):
        print(i+1,ans[i])     from collections import deque if __name__ == "__main__":
    n = int(input())
    graph = []
    for _ in range(n):
        v, k, *tmp = map(int, input().split())
        for j in range(len(tmp)):
            tmp[j] -= 1
        graph.append(tmp)
    visited = [False] * n
    qu = deque()
    qu.appendleft((0, 0))
    ans = [-1] * n
    visited[0] = True

    while qu:
        now, dis = qu.pop()
        ans[now] = dis

        for nei in graph[now]:
            if not visited[nei]:
                visited[nei] = True
                qu.appendleft((nei, dis + 1))
    for i in range(n):
        print(i+1,ans[i])     __name__ == "__main__" __name__ "__main__" n = int(input()) n int(input()) int input() input graph = [] graph [] for _ in range(n):
        v, k, *tmp = map(int, input().split())
        for j in range(len(tmp)):
            tmp[j] -= 1
        graph.append(tmp)
     _ range(n) range n v, k, *tmp = map(int, input().split()) v map(int, input().split()) map int input().split() ().split () input split k *tmp tmp for j in range(len(tmp)):
            tmp[j] -= 1
         j range(len(tmp)) range len(tmp) len tmp tmp[j] -= 1 tmp[j] tmp j 1 graph.append(tmp) graph.append graph append tmp visited = [False] * n visited [False] * n [False] False n qu = deque() qu deque() deque qu.appendleft((0, 0)) qu.appendleft qu appendleft (0, 0) 0 0 ans = [-1] * n ans [-1] * n [-1] -1 n visited[0] = True visited[0] visited 0 True while qu:
        now, dis = qu.pop()
        ans[now] = dis

        for nei in graph[now]:
            if not visited[nei]:
                visited[nei] = True
                qu.appendleft((nei, dis + 1))
     qu now, dis = qu.pop() now qu.pop() qu.pop qu pop dis ans[now] = dis ans[now] ans now dis for nei in graph[now]:
            if not visited[nei]:
                visited[nei] = True
                qu.appendleft((nei, dis + 1))
     nei graph[now] graph now if not visited[nei]:
                visited[nei] = True
                qu.appendleft((nei, dis + 1))
     not visited[nei] visited[nei] visited nei visited[nei] = True visited[nei] visited nei True qu.appendleft((nei, dis + 1)) qu.appendleft qu appendleft (nei, dis + 1) nei dis + 1 dis 1 for i in range(n):
        print(i+1,ans[i])     i range(n) range n print(i+1,ans[i]) print i+1 i 1 ans[i] ans i visited = [False] * n [False] * n visited graph = [] [] graph now, dis = qu.pop() qu.pop() now dis = qu.pop() qu.pop() dis k, *tmp = map(int, input().split()) map(int, input().split()) k n = int(input()) int(input()) n ans = [-1] * n [-1] * n ans v, k, *tmp = map(int, input().split()) map(int, input().split()) v qu = deque() deque() qu
from collections import deque
if __name__ == "__main__":
    n = int(input())
    graph = []
    for _ in range(n):
        v, k, *tmp = map(int, input().split())
        for j in range(len(tmp)):
            tmp[j] -= 1
        graph.append(tmp)
    visited = [False] * n
    qu = deque()
    qu.appendleft((0, 0))
    ans = [-1] * n
    visited[0] = True

    while qu:
        now, dis = qu.pop()
        ans[now] = dis

        for nei in graph[now]:
            if not visited[nei]:
                visited[nei] = True
                qu.appendleft((nei, dis + 1))
    for i in range(n):
        print(i+1,ans[i])    
