import sys
n = int(input())
input_line = [[int(i) for i in str.split()] for str in sys.stdin.read().splitlines()]
for i in range(n):
    input_line[i] = [] if len(input_line[i]) == 2 else input_line[i][2:]
is_visit = [False] * n
distance = [0] * n
queue = [1]

while len(queue) != 0:
    id = queue.pop(0)
    is_visit[id-1] = True
    for node_id in input_line[id-1]:
        if not is_visit[node_id-1]:
            distance[node_id-1] = distance[id-1] + 1
            queue.append(node_id)
            is_visit[node_id-1] = True

for i in range(n):
    d = distance[i] if is_visit[i] else -1
    print('{0} {1}'.format(i+1, d))



 import sys sys n = int(input()) n int(input()) int input() input [int(i) for i in str.split()] for str in sys.stdin.read().splitlines() for str in sys.stdin.read().splitlines() str sys.stdin.read().splitlines() sys.stdin.read().splitlines sys.stdin.read() sys.stdin.read sys.stdin sys stdin read splitlines for str in sys.stdin.read().splitlines() [int(i) for i in str.split()] input_line = [[int(i) for i in str.split()] for str in sys.stdin.read().splitlines()] input_line [[int(i) for i in str.split()] for str in sys.stdin.read().splitlines()] for i in range(n):
    input_line[i] = [] if len(input_line[i]) == 2 else input_line[i][2:] i range(n) range n input_line[i] = [] if len(input_line[i]) == 2 else input_line[i][2:] input_line[i] input_line i [] if len(input_line[i]) == 2 else input_line[i][2:] len(input_line[i]) == 2 len(input_line[i]) len input_line[i] input_line i 2 [] input_line[i][2:] [i] input_line i 2: 2 is_visit = [False] * n is_visit [False] * n [False] False n distance = [0] * n distance [0] * n [0] 0 n queue = [1] queue [1] 1 while len(queue) != 0:
    id = queue.pop(0)
    is_visit[id-1] = True
    for node_id in input_line[id-1]:
        if not is_visit[node_id-1]:
            distance[node_id-1] = distance[id-1] + 1
            queue.append(node_id)
            is_visit[node_id-1] = True len(queue) != 0 len(queue) len queue 0 id = queue.pop(0) id queue.pop(0) queue.pop queue pop 0 is_visit[id-1] = True is_visit[id-1] is_visit id-1 id 1 True for node_id in input_line[id-1]:
        if not is_visit[node_id-1]:
            distance[node_id-1] = distance[id-1] + 1
            queue.append(node_id)
            is_visit[node_id-1] = True node_id input_line[id-1] input_line id-1 id 1 if not is_visit[node_id-1]:
            distance[node_id-1] = distance[id-1] + 1
            queue.append(node_id)
            is_visit[node_id-1] = True not is_visit[node_id-1] is_visit[node_id-1] is_visit node_id-1 node_id 1 distance[node_id-1] = distance[id-1] + 1 distance[node_id-1] distance node_id-1 node_id 1 distance[id-1] + 1 distance[id-1] distance id-1 id 1 1 queue.append(node_id) queue.append queue append node_id is_visit[node_id-1] = True is_visit[node_id-1] is_visit node_id-1 node_id 1 True for i in range(n):
    d = distance[i] if is_visit[i] else -1
    print('{0} {1}'.format(i+1, d)) i range(n) range n d = distance[i] if is_visit[i] else -1 d distance[i] if is_visit[i] else -1 is_visit[i] is_visit i distance[i] distance i -1 print('{0} {1}'.format(i+1, d)) print '{0} {1}'.format(i+1, d) '{0} {1}'.format '{0} {1}' format i+1 i 1 d distance = [0] * n [0] * n distance queue = [1] [1] queue id = queue.pop(0) queue.pop(0) id n = int(input()) int(input()) n d = distance[i] if is_visit[i] else -1 distance[i] if is_visit[i] else -1 d input_line = [[int(i) for i in str.split()] for str in sys.stdin.read().splitlines()] [[int(i) for i in str.split()] for str in sys.stdin.read().splitlines()] input_line is_visit = [False] * n [False] * n is_visit
import sys
n = int(input())
input_line = [[int(i) for i in str.split()] for str in sys.stdin.read().splitlines()]
for i in range(n):
    input_line[i] = [] if len(input_line[i]) == 2 else input_line[i][2:]
is_visit = [False] * n
distance = [0] * n
queue = [1]

while len(queue) != 0:
    id = queue.pop(0)
    is_visit[id-1] = True
    for node_id in input_line[id-1]:
        if not is_visit[node_id-1]:
            distance[node_id-1] = distance[id-1] + 1
            queue.append(node_id)
            is_visit[node_id-1] = True

for i in range(n):
    d = distance[i] if is_visit[i] else -1
    print('{0} {1}'.format(i+1, d))




