n = int(input())

adj = [None]
for i in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i)

isSearched = [None] + [False] * n

distance = [None] + [-1] * n

import collections

def bfs(u):
    isSearched[u] = True
    distance[u] = 0
    edge = collections.deque()
    edge.append(u)
    while edge:
        c_e = edge.popleft()
        for n_e in adj[c_e]:
            if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e)


bfs(1)

for i, x in enumerate(distance[1:], start=1):
    print(i, x) n = int(input()) n int(input()) int input() input adj = [None] adj [None] None for i in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i) i range(n) range n adj_i = list(map(int, input().split()[2:])) adj_i list(map(int, input().split()[2:])) list map(int, input().split()[2:]) map int input().split()[2:] ().split() ().split () input split 2: 2 adj.append(adj_i) adj.append adj append adj_i isSearched = [None] + [False] * n isSearched [None] + [False] * n [None] None [False] * n [False] False n distance = [None] + [-1] * n distance [None] + [-1] * n [None] None [-1] * n [-1] -1 n import collections collections def bfs(u):
    isSearched[u] = True
    distance[u] = 0
    edge = collections.deque()
    edge.append(u)
    while edge:
        c_e = edge.popleft()
        for n_e in adj[c_e]:
            if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e) bfs isSearched[u] = True isSearched[u] isSearched u True distance[u] = 0 distance[u] distance u 0 edge = collections.deque() edge collections.deque() collections.deque collections deque edge.append(u) edge.append edge append u while edge:
        c_e = edge.popleft()
        for n_e in adj[c_e]:
            if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e) edge c_e = edge.popleft() c_e edge.popleft() edge.popleft edge popleft for n_e in adj[c_e]:
            if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e) n_e adj[c_e] adj c_e if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e) not isSearched[n_e] isSearched[n_e] isSearched n_e isSearched[n_e] = True isSearched[n_e] isSearched n_e True distance[n_e] = distance[c_e] + 1 distance[n_e] distance n_e distance[c_e] + 1 distance[c_e] distance c_e 1 edge.append(n_e) edge.append edge append n_e u u bfs(1) bfs 1 for i, x in enumerate(distance[1:], start=1):
    print(i, x) i x enumerate(distance[1:], start=1) enumerate distance[1:] distance 1: 1 start=1 start 1 print(i, x) print i x isSearched = [None] + [False] * n [None] + [False] * n isSearched adj = [None] [None] adj def bfs(u):
    isSearched[u] = True
    distance[u] = 0
    edge = collections.deque()
    edge.append(u)
    while edge:
        c_e = edge.popleft()
        for n_e in adj[c_e]:
            if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e) def bfs(u):
    isSearched[u] = True
    distance[u] = 0
    edge = collections.deque()
    edge.append(u)
    while edge:
        c_e = edge.popleft()
        for n_e in adj[c_e]:
            if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e) bfs adj_i = list(map(int, input().split()[2:])) list(map(int, input().split()[2:])) adj_i n = int(input()) int(input()) n distance = [None] + [-1] * n [None] + [-1] * n distance
n = int(input())

adj = [None]
for i in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i)

isSearched = [None] + [False] * n

distance = [None] + [-1] * n

import collections

def bfs(u):
    isSearched[u] = True
    distance[u] = 0
    edge = collections.deque()
    edge.append(u)
    while edge:
        c_e = edge.popleft()
        for n_e in adj[c_e]:
            if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e)


bfs(1)

for i, x in enumerate(distance[1:], start=1):
    print(i, x)