n = int(input())

graphs = []

for _ in range(n):
    graphs.append(list(map(int, input().split()))[2:])


def bfs(graphs, seen_points, score, scores, queue):
    while len(queue) > 0:
        start_point = queue.pop(0)
        if seen_points[start_point - 1] == 2:
            continue
        else:
            next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0]
            if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2


seen_points = [0 for _ in range(n)]
scores = [-1 for _ in range(n)]
scores[0] = 0
queue = [1]
bfs(graphs, seen_points, 0, scores, queue)

for index, score in enumerate(scores):
    print(index + 1, score)
 n = int(input()) n int(input()) int input() input graphs = [] graphs [] for _ in range(n):
    graphs.append(list(map(int, input().split()))[2:]) _ range(n) range n graphs.append(list(map(int, input().split()))[2:]) graphs.append graphs append list(map(int, input().split()))[2:] (map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split 2: 2 def bfs(graphs, seen_points, score, scores, queue):
    while len(queue) > 0:
        start_point = queue.pop(0)
        if seen_points[start_point - 1] == 2:
            continue
        else:
            next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0]
            if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2 bfs while len(queue) > 0:
        start_point = queue.pop(0)
        if seen_points[start_point - 1] == 2:
            continue
        else:
            next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0]
            if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2 len(queue) > 0 len(queue) len queue 0 start_point = queue.pop(0) start_point queue.pop(0) queue.pop queue pop 0 if seen_points[start_point - 1] == 2:
            continue
        else:
            next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0]
            if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2 seen_points[start_point - 1] == 2 seen_points[start_point - 1] seen_points start_point - 1 start_point 1 2 continue point for point in graphs[start_point - 1] if seen_points[point - 1] == 0 for point in graphs[start_point - 1] if seen_points[point - 1] == 0 point graphs[start_point - 1] graphs start_point - 1 start_point 1 seen_points[point - 1] == 0 seen_points[point - 1] seen_points point - 1 point 1 0 if seen_points[point - 1] == 0 point next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0] next_points [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0] if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2 len(next_points) >= 1 len(next_points) len next_points 1 score += 1 score 1 for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                 point next_points queue.append(point) queue.append queue append point if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                 seen_points[point - 1] == 0 seen_points[point - 1] seen_points point - 1 point 1 0 seen_points[point - 1] = 1 seen_points[point - 1] seen_points point - 1 point 1 1 scores[point - 1] = scores[start_point - 1] + 1 scores[point - 1] scores point - 1 point 1 scores[start_point - 1] + 1 scores[start_point - 1] scores start_point - 1 start_point 1 1 seen_points[start_point - 1] = 2 seen_points[start_point - 1] seen_points start_point - 1 start_point 1 2 graphs graphs seen_points seen_points score score scores scores queue queue 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 seen_points = [0 for _ in range(n)] seen_points [0 for _ in range(n)] -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 scores = [-1 for _ in range(n)] scores [-1 for _ in range(n)] scores[0] = 0 scores[0] scores 0 0 queue = [1] queue [1] 1 bfs(graphs, seen_points, 0, scores, queue) bfs graphs seen_points 0 scores queue for index, score in enumerate(scores):
    print(index + 1, score) index score enumerate(scores) enumerate scores print(index + 1, score) print index + 1 index 1 score seen_points = [0 for _ in range(n)] [0 for _ in range(n)] seen_points queue = [1] [1] queue graphs = [] [] graphs n = int(input()) int(input()) n scores = [-1 for _ in range(n)] [-1 for _ in range(n)] scores def bfs(graphs, seen_points, score, scores, queue):
    while len(queue) > 0:
        start_point = queue.pop(0)
        if seen_points[start_point - 1] == 2:
            continue
        else:
            next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0]
            if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2 def bfs(graphs, seen_points, score, scores, queue):
    while len(queue) > 0:
        start_point = queue.pop(0)
        if seen_points[start_point - 1] == 2:
            continue
        else:
            next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0]
            if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2 bfs
n = int(input())

graphs = []

for _ in range(n):
    graphs.append(list(map(int, input().split()))[2:])


def bfs(graphs, seen_points, score, scores, queue):
    while len(queue) > 0:
        start_point = queue.pop(0)
        if seen_points[start_point - 1] == 2:
            continue
        else:
            next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0]
            if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2


seen_points = [0 for _ in range(n)]
scores = [-1 for _ in range(n)]
scores[0] = 0
queue = [1]
bfs(graphs, seen_points, 0, scores, queue)

for index, score in enumerate(scores):
    print(index + 1, score)

