from collections import deque


def bfs(start):
    Q = deque()
    Q.append(start)
    dist[start] = 0

    while Q:
        v = Q.popleft()

        for nv in G[v]:
            if dist[nv] != -1:
                continue

            # print(Q)
            dist[nv] = dist[v] + 1
            Q.append(nv)


N = int(input())

G = [[]] + [list(map(int, input().split())) for _ in range(N)]
for a in range(1, N + 1):
    b = G[a][2:]
    G[a] = b

# print(G)
dist = [-1 for _ in range(N + 1)]

start = 1
bfs(start)
# print(dist[1:])
for i in range(1, N + 1):
    print(i, dist[i])
 from collections import deque def bfs(start):
    Q = deque()
    Q.append(start)
    dist[start] = 0

    while Q:
        v = Q.popleft()

        for nv in G[v]:
            if dist[nv] != -1:
                continue

            # print(Q)
            dist[nv] = dist[v] + 1
            Q.append(nv) bfs Q = deque() Q deque() deque Q.append(start) Q.append Q append start dist[start] = 0 dist[start] dist start 0 while Q:
        v = Q.popleft()

        for nv in G[v]:
            if dist[nv] != -1:
                continue

            # print(Q)
            dist[nv] = dist[v] + 1
            Q.append(nv) Q v = Q.popleft() v Q.popleft() Q.popleft Q popleft for nv in G[v]:
            if dist[nv] != -1:
                continue

            # print(Q)
            dist[nv] = dist[v] + 1
            Q.append(nv) nv G[v] G v if dist[nv] != -1:
                continue

            # print(Q)
             dist[nv] != -1 dist[nv] dist nv -1 continue dist[nv] = dist[v] + 1 dist[nv] dist nv dist[v] + 1 dist[v] dist v 1 Q.append(nv) Q.append Q append nv start start N = int(input()) N int(input()) int input() input list(map(int, input().split())) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split G = [[]] + [list(map(int, input().split())) for _ in range(N)] G [[]] + [list(map(int, input().split())) for _ in range(N)] [[]] [] [list(map(int, input().split())) for _ in range(N)] for a in range(1, N + 1):
    b = G[a][2:]
    G[a] = b

# print(G) a range(1, N + 1) range 1 N + 1 N 1 b = G[a][2:] b G[a][2:] [a] G a 2: 2 G[a] = b G[a] G a b -1 for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) -1 dist = [-1 for _ in range(N + 1)] dist [-1 for _ in range(N + 1)] start = 1 start 1 bfs(start) bfs start for i in range(1, N + 1):
    print(i, dist[i]) i range(1, N + 1) range 1 N + 1 N 1 print(i, dist[i]) print i dist[i] dist i start = 1 1 start dist = [-1 for _ in range(N + 1)] [-1 for _ in range(N + 1)] dist G = [[]] + [list(map(int, input().split())) for _ in range(N)] [[]] + [list(map(int, input().split())) for _ in range(N)] G def bfs(start):
    Q = deque()
    Q.append(start)
    dist[start] = 0

    while Q:
        v = Q.popleft()

        for nv in G[v]:
            if dist[nv] != -1:
                continue

            # print(Q)
            dist[nv] = dist[v] + 1
            Q.append(nv) def bfs(start):
    Q = deque()
    Q.append(start)
    dist[start] = 0

    while Q:
        v = Q.popleft()

        for nv in G[v]:
            if dist[nv] != -1:
                continue

            # print(Q)
            dist[nv] = dist[v] + 1
            Q.append(nv) bfs N = int(input()) int(input()) N b = G[a][2:] G[a][2:] b
from collections import deque


def bfs(start):
    Q = deque()
    Q.append(start)
    dist[start] = 0

    while Q:
        v = Q.popleft()

        for nv in G[v]:
            if dist[nv] != -1:
                continue

            # print(Q)
            dist[nv] = dist[v] + 1
            Q.append(nv)


N = int(input())

G = [[]] + [list(map(int, input().split())) for _ in range(N)]
for a in range(1, N + 1):
    b = G[a][2:]
    G[a] = b

# print(G)
dist = [-1 for _ in range(N + 1)]

start = 1
bfs(start)
# print(dist[1:])
for i in range(1, N + 1):
    print(i, dist[i])

