import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *V = map(int, input().split())
        for v in V:
            edge[u - 1].append(v - 1)

    depth = [f_inf] * n
    depth[0] = 0
    que = deque([0])
    while que:
        v = que.popleft()
        for u in edge[v]:
            if depth[u] == f_inf:
                depth[u] = depth[v] + 1
                que.append(u)

    for i in range(n):
        print(i + 1, depth[i] if depth[i] != f_inf else -1)


if __name__ == '__main__':
    resolve()
 import sys sys from collections import deque sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline f_inf = float('inf') f_inf float('inf') float 'inf' mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 def resolve():
    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *V = map(int, input().split())
        for v in V:
            edge[u - 1].append(v - 1)

    depth = [f_inf] * n
    depth[0] = 0
    que = deque([0])
    while que:
        v = que.popleft()
        for u in edge[v]:
            if depth[u] == f_inf:
                depth[u] = depth[v] + 1
                que.append(u)

    for i in range(n):
        print(i + 1, depth[i] if depth[i] != f_inf else -1) resolve n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] edge = [[] for _ in range(n)] edge [[] for _ in range(n)] for _ in range(n):
        u, k, *V = map(int, input().split())
        for v in V:
            edge[u - 1].append(v - 1)

     _ range(n) range n u, k, *V = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *V V for v in V:
            edge[u - 1].append(v - 1)

     v V edge[u - 1].append(v - 1) [u - 1].append [u - 1] edge u - 1 u 1 append v - 1 v 1 depth = [f_inf] * n depth [f_inf] * n [f_inf] f_inf n depth[0] = 0 depth[0] depth 0 0 que = deque([0]) que deque([0]) deque [0] 0 while que:
        v = que.popleft()
        for u in edge[v]:
            if depth[u] == f_inf:
                depth[u] = depth[v] + 1
                que.append(u)

     que v = que.popleft() v que.popleft() que.popleft que popleft for u in edge[v]:
            if depth[u] == f_inf:
                depth[u] = depth[v] + 1
                que.append(u)

     u edge[v] edge v if depth[u] == f_inf:
                depth[u] = depth[v] + 1
                que.append(u)

     depth[u] == f_inf depth[u] depth u f_inf depth[u] = depth[v] + 1 depth[u] depth u depth[v] + 1 depth[v] depth v 1 que.append(u) que.append que append u for i in range(n):
        print(i + 1, depth[i] if depth[i] != f_inf else -1) i range(n) range n print(i + 1, depth[i] if depth[i] != f_inf else -1) print i + 1 i 1 depth[i] if depth[i] != f_inf else -1 depth[i] != f_inf depth[i] depth i f_inf depth[i] depth i -1 if __name__ == '__main__':
    resolve() __name__ == '__main__' __name__ '__main__' resolve() resolve mod = 10 ** 9 + 7 10 ** 9 + 7 mod input = sys.stdin.readline sys.stdin.readline input f_inf = float('inf') float('inf') f_inf def resolve():
    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *V = map(int, input().split())
        for v in V:
            edge[u - 1].append(v - 1)

    depth = [f_inf] * n
    depth[0] = 0
    que = deque([0])
    while que:
        v = que.popleft()
        for u in edge[v]:
            if depth[u] == f_inf:
                depth[u] = depth[v] + 1
                que.append(u)

    for i in range(n):
        print(i + 1, depth[i] if depth[i] != f_inf else -1) def resolve():
    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *V = map(int, input().split())
        for v in V:
            edge[u - 1].append(v - 1)

    depth = [f_inf] * n
    depth[0] = 0
    que = deque([0])
    while que:
        v = que.popleft()
        for u in edge[v]:
            if depth[u] == f_inf:
                depth[u] = depth[v] + 1
                que.append(u)

    for i in range(n):
        print(i + 1, depth[i] if depth[i] != f_inf else -1) resolve
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *V = map(int, input().split())
        for v in V:
            edge[u - 1].append(v - 1)

    depth = [f_inf] * n
    depth[0] = 0
    que = deque([0])
    while que:
        v = que.popleft()
        for u in edge[v]:
            if depth[u] == f_inf:
                depth[u] = depth[v] + 1
                que.append(u)

    for i in range(n):
        print(i + 1, depth[i] if depth[i] != f_inf else -1)


if __name__ == '__main__':
    resolve()

