from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def bfs(graph, initial):
    n = len(graph) - 1
    dist = [-1] * (n + 1)
    q = deque([initial])
    visited = [False] * (n + 1)
    visited[initial] = True
    dist[initial] = 0

    while len(q) != 0:
        edge = q.popleft()
        nxt = graph[edge]

        for i, e in enumerate(nxt):
            if visited[e] is False:
                q.append(e)
                dist[e] = dist[edge] + 1
                visited[e] = True

    return dist


def main():
    N = I()
    graph = [[] for i in range(N+1)]

    for i in range(N):
        a = LI()
        edge = a[0]
        if a[1] == 0:
            continue
        for e in a[2:]:
            graph[edge].append(e)
            # graph[e].append(edge)

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


if __name__ == '__main__':
    main()
 from heapq import heappush, heappop, heapify from collections import deque, defaultdict, Counter import itertools itertools from itertools import permutations, combinations, accumulate import sys sys import bisect bisect import string string import math math import time time def I(): return int(input()) I def S(): return input() S def MI(): return map(int, input().split()) MI def MS(): return map(str, input().split()) MS int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i def LI(): return [int(i) for i in input().split()] LI int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 def LI_(): return [int(i)-1 for i in input().split()] LI_ ord(i)-97 for i in input() for i in input() i input() input for i in input() ord(i)-97 ord(i) ord i 97 def StoI(): return [ord(i)-97 for i in input()] StoI def ItoS(nn): return chr(nn+97) ItoS nn nn def input(): return sys.stdin.readline().rstrip() input def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end) show if show_flg:
        print(*inp, end=end) show_flg print(*inp, end=end) print *inp inp end=end end end end='\n' end '\n' *inp YNL = {False: 'No', True: 'Yes'} YNL {False: 'No', True: 'Yes'} False False 'No' True True 'Yes' YNU = {False: 'NO', True: 'YES'} YNU {False: 'NO', True: 'YES'} False False 'NO' True True 'YES' MOD = 10**9+7 MOD 10**9+7 10**9 10 9 7 inf = float('inf') inf float('inf') float 'inf' IINF = 10**10 IINF 10**10 10 10 l_alp = string.ascii_lowercase l_alp string.ascii_lowercase string ascii_lowercase u_alp = string.ascii_uppercase u_alp string.ascii_uppercase string ascii_uppercase ts = time.time() ts time.time() time.time time time sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] nums ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] '1' '2' '3' '4' '5' '6' '7' '8' '9' '10' show_flg = False show_flg False def bfs(graph, initial):
    n = len(graph) - 1
    dist = [-1] * (n + 1)
    q = deque([initial])
    visited = [False] * (n + 1)
    visited[initial] = True
    dist[initial] = 0

    while len(q) != 0:
        edge = q.popleft()
        nxt = graph[edge]

        for i, e in enumerate(nxt):
            if visited[e] is False:
                q.append(e)
                dist[e] = dist[edge] + 1
                visited[e] = True

    return dist bfs n = len(graph) - 1 n len(graph) - 1 len(graph) len graph 1 dist = [-1] * (n + 1) dist [-1] * (n + 1) [-1] -1 n + 1 n 1 q = deque([initial]) q deque([initial]) deque [initial] initial visited = [False] * (n + 1) visited [False] * (n + 1) [False] False n + 1 n 1 visited[initial] = True visited[initial] visited initial True dist[initial] = 0 dist[initial] dist initial 0 while len(q) != 0:
        edge = q.popleft()
        nxt = graph[edge]

        for i, e in enumerate(nxt):
            if visited[e] is False:
                q.append(e)
                dist[e] = dist[edge] + 1
                visited[e] = True

     len(q) != 0 len(q) len q 0 edge = q.popleft() edge q.popleft() q.popleft q popleft nxt = graph[edge] nxt graph[edge] graph edge for i, e in enumerate(nxt):
            if visited[e] is False:
                q.append(e)
                dist[e] = dist[edge] + 1
                visited[e] = True

     i e enumerate(nxt) enumerate nxt if visited[e] is False:
                q.append(e)
                dist[e] = dist[edge] + 1
                visited[e] = True

     visited[e] is False visited[e] visited e False q.append(e) q.append q append e dist[e] = dist[edge] + 1 dist[e] dist e dist[edge] + 1 dist[edge] dist edge 1 visited[e] = True visited[e] visited e True return dist dist graph graph initial initial def main():
    N = I()
    graph = [[] for i in range(N+1)]

    for i in range(N):
        a = LI()
        edge = a[0]
        if a[1] == 0:
            continue
        for e in a[2:]:
            graph[edge].append(e)
            # graph[e].append(edge)

    # print(graph)
    ret = bfs(graph, 1)
    for i in range(1, N+1):
        print(i, ret[i]) main N = I() N I() I [] for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) [] graph = [[] for i in range(N+1)] graph [[] for i in range(N+1)] for i in range(N):
        a = LI()
        edge = a[0]
        if a[1] == 0:
            continue
        for e in a[2:]:
            graph[edge].append(e)
            # graph[e].append(edge)

    # print(graph)
     i range(N) range N a = LI() a LI() LI edge = a[0] edge a[0] a 0 if a[1] == 0:
            continue
         a[1] == 0 a[1] a 1 0 continue for e in a[2:]:
            graph[edge].append(e)
            # graph[e].append(edge)

    # print(graph)
     e a[2:] a 2: 2 graph[edge].append(e) [edge].append [edge] graph edge append e ret = bfs(graph, 1) ret bfs(graph, 1) bfs graph 1 for i in range(1, N+1):
        print(i, ret[i]) i range(1, N+1) range 1 N+1 N 1 print(i, ret[i]) print i ret[i] ret i if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    N = I()
    graph = [[] for i in range(N+1)]

    for i in range(N):
        a = LI()
        edge = a[0]
        if a[1] == 0:
            continue
        for e in a[2:]:
            graph[edge].append(e)
            # graph[e].append(edge)

    # print(graph)
    ret = bfs(graph, 1)
    for i in range(1, N+1):
        print(i, ret[i]) def main():
    N = I()
    graph = [[] for i in range(N+1)]

    for i in range(N):
        a = LI()
        edge = a[0]
        if a[1] == 0:
            continue
        for e in a[2:]:
            graph[edge].append(e)
            # graph[e].append(edge)

    # print(graph)
    ret = bfs(graph, 1)
    for i in range(1, N+1):
        print(i, ret[i]) main nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] nums l_alp = string.ascii_lowercase string.ascii_lowercase l_alp def MS(): return map(str, input().split()) def MS(): return map(str, input().split()) MS def input(): return sys.stdin.readline().rstrip() def input(): return sys.stdin.readline().rstrip() input MOD = 10**9+7 10**9+7 MOD def LI_(): return [int(i)-1 for i in input().split()] def LI_(): return [int(i)-1 for i in input().split()] LI_ def LI(): return [int(i) for i in input().split()] def LI(): return [int(i) for i in input().split()] LI IINF = 10**10 10**10 IINF def StoI(): return [ord(i)-97 for i in input()] def StoI(): return [ord(i)-97 for i in input()] StoI def I(): return int(input()) def I(): return int(input()) I def ItoS(nn): return chr(nn+97) def ItoS(nn): return chr(nn+97) ItoS def S(): return input() def S(): return input() S def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end) def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end) show u_alp = string.ascii_uppercase string.ascii_uppercase u_alp YNU = {False: 'NO', True: 'YES'} {False: 'NO', True: 'YES'} YNU def bfs(graph, initial):
    n = len(graph) - 1
    dist = [-1] * (n + 1)
    q = deque([initial])
    visited = [False] * (n + 1)
    visited[initial] = True
    dist[initial] = 0

    while len(q) != 0:
        edge = q.popleft()
        nxt = graph[edge]

        for i, e in enumerate(nxt):
            if visited[e] is False:
                q.append(e)
                dist[e] = dist[edge] + 1
                visited[e] = True

    return dist def bfs(graph, initial):
    n = len(graph) - 1
    dist = [-1] * (n + 1)
    q = deque([initial])
    visited = [False] * (n + 1)
    visited[initial] = True
    dist[initial] = 0

    while len(q) != 0:
        edge = q.popleft()
        nxt = graph[edge]

        for i, e in enumerate(nxt):
            if visited[e] is False:
                q.append(e)
                dist[e] = dist[edge] + 1
                visited[e] = True

    return dist bfs def MI(): return map(int, input().split()) def MI(): return map(int, input().split()) MI ts = time.time() time.time() ts YNL = {False: 'No', True: 'Yes'} {False: 'No', True: 'Yes'} YNL inf = float('inf') float('inf') inf show_flg = False False show_flg
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def bfs(graph, initial):
    n = len(graph) - 1
    dist = [-1] * (n + 1)
    q = deque([initial])
    visited = [False] * (n + 1)
    visited[initial] = True
    dist[initial] = 0

    while len(q) != 0:
        edge = q.popleft()
        nxt = graph[edge]

        for i, e in enumerate(nxt):
            if visited[e] is False:
                q.append(e)
                dist[e] = dist[edge] + 1
                visited[e] = True

    return dist


def main():
    N = I()
    graph = [[] for i in range(N+1)]

    for i in range(N):
        a = LI()
        edge = a[0]
        if a[1] == 0:
            continue
        for e in a[2:]:
            graph[edge].append(e)
            # graph[e].append(edge)

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


if __name__ == '__main__':
    main()

