from collections import deque


def bfs(root):
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        i = Q.popleft()
        for v in range(n):
            if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black'


n = int(input())
adjMat = [[0] * n for _ in range(n)]
color = ['white'] * n
distance = [-1] * n

for _ in range(n):
    adj = list(map(int, input().split()))
    i = adj[0]
    v = adj[2:]
    for j in v:
        adjMat[i - 1][j - 1] = 1

bfs(0)

for i in range(n):
    out = ''
    out += '{} {}'.format(i + 1, distance[i])
    print(out) from collections import deque def bfs(root):
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        i = Q.popleft()
        for v in range(n):
            if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black' bfs Q = deque() Q deque() deque distance[root] = 0 distance[root] distance root 0 Q.append(root) Q.append Q append root i = root i root while len(Q) > 0:
        i = Q.popleft()
        for v in range(n):
            if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black' len(Q) > 0 len(Q) len Q 0 i = Q.popleft() i Q.popleft() Q.popleft Q popleft for v in range(n):
            if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
         v range(n) range n if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
         adjMat[i][v] == 1 and color[v] == 'white' adjMat[i][v] == 1 adjMat[i][v] [i] adjMat i v 1 color[v] == 'white' color[v] color v 'white' Q.append(v) Q.append Q append v color[v] = 'gray' color[v] color v 'gray' distance[v] = distance[i] + 1 distance[v] distance v distance[i] + 1 distance[i] distance i 1 color[i] = 'black' color[i] color i 'black' root root n = int(input()) n int(input()) int input() input [0] * n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * n [0] 0 n adjMat = [[0] * n for _ in range(n)] adjMat [[0] * n for _ in range(n)] color = ['white'] * n color ['white'] * n ['white'] 'white' n distance = [-1] * n distance [-1] * n [-1] -1 n for _ in range(n):
    adj = list(map(int, input().split()))
    i = adj[0]
    v = adj[2:]
    for j in v:
        adjMat[i - 1][j - 1] = 1 _ range(n) range n adj = list(map(int, input().split())) adj list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split i = adj[0] i adj[0] adj 0 v = adj[2:] v adj[2:] adj 2: 2 for j in v:
        adjMat[i - 1][j - 1] = 1 j v adjMat[i - 1][j - 1] = 1 adjMat[i - 1][j - 1] [i - 1] adjMat i - 1 i 1 j - 1 j 1 1 bfs(0) bfs 0 for i in range(n):
    out = ''
    out += '{} {}'.format(i + 1, distance[i])
    print(out) i range(n) range n out = '' out '' out += '{} {}'.format(i + 1, distance[i]) out '{} {}'.format(i + 1, distance[i]) '{} {}'.format '{} {}' format i + 1 i 1 distance[i] distance i print(out) print out def bfs(root):
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        i = Q.popleft()
        for v in range(n):
            if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black' def bfs(root):
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        i = Q.popleft()
        for v in range(n):
            if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black' bfs i = adj[0] adj[0] i out = '' '' out out += '{} {}'.format(i + 1, distance[i]) '{} {}'.format(i + 1, distance[i]) out n = int(input()) int(input()) n adj = list(map(int, input().split())) list(map(int, input().split())) adj color = ['white'] * n ['white'] * n color v = adj[2:] adj[2:] v adjMat = [[0] * n for _ in range(n)] [[0] * n for _ in range(n)] adjMat distance = [-1] * n [-1] * n distance
from collections import deque


def bfs(root):
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        i = Q.popleft()
        for v in range(n):
            if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black'


n = int(input())
adjMat = [[0] * n for _ in range(n)]
color = ['white'] * n
distance = [-1] * n

for _ in range(n):
    adj = list(map(int, input().split()))
    i = adj[0]
    v = adj[2:]
    for j in v:
        adjMat[i - 1][j - 1] = 1

bfs(0)

for i in range(n):
    out = ''
    out += '{} {}'.format(i + 1, distance[i])
    print(out)