import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect

# from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

d = [-1] * n

for a in adj:
    for i in range(len(a)):
        a[i] -= 1

s = 0
q = collections.deque([])
d[s] = 0
q.append(s)
while len(q):
    u = q.popleft()
    v = adj[u]
    for vi in v:
        if d[vi] == -1:
            d[vi] = d[u] + 1
            q.append(vi)
        elif d[vi] > d[u] + 1:
            d[vi] = d[u] + 1
            q.append(vi)

for i, di in enumerate(d):
    print(i + 1, di)
 import math math import copy copy import sys sys import fractions fractions import decimal decimal import heapq heapq import collections collections import itertools itertools import bisect bisect sys.setrecursionlimit(100001) sys.setrecursionlimit sys setrecursionlimit 100001 def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList getInputIntList outputDataList = [] outputDataList [] inputData = input().split() inputData input().split() ().split () input split int(n) for n in inputData for n in inputData n inputData for n in inputData int(n) int n outputDataList = [int(n) for n in inputData] outputDataList [int(n) for n in inputData] return outputDataList outputDataList def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList getSomeInputInt outputDataList = [] outputDataList [] for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

     i range(n) range n inputData = int(input()) inputData int(input()) int input() input outputDataList.append(inputData) outputDataList.append outputDataList append inputData return outputDataList outputDataList n n def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE=== getSomeInputListInt inputDataList = [] inputDataList [] outputDataList = [] outputDataList [] for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

     i range(n) range n inputData = input().split() inputData input().split() ().split () input split int(n) for n in inputData for n in inputData n inputData for n in inputData int(n) int n inputDataList = [int(n) for n in inputData] inputDataList [int(n) for n in inputData] outputDataList.append(inputDataList) outputDataList.append outputDataList append inputDataList return outputDataList outputDataList n n n = int(input()) n int(input()) int input() input list(map(int, input().split()))[2:] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int, input().split()))[2:] (map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split 2: 2 adj = [list(map(int, input().split()))[2:] for _ in range(n)] adj [list(map(int, input().split()))[2:] for _ in range(n)] d = [-1] * n d [-1] * n [-1] -1 n for a in adj:
    for i in range(len(a)):
        a[i] -= 1 a adj for i in range(len(a)):
        a[i] -= 1 i range(len(a)) range len(a) len a a[i] -= 1 a[i] a i 1 s = 0 s 0 q = collections.deque([]) q collections.deque([]) collections.deque collections deque [] d[s] = 0 d[s] d s 0 q.append(s) q.append q append s while len(q):
    u = q.popleft()
    v = adj[u]
    for vi in v:
        if d[vi] == -1:
            d[vi] = d[u] + 1
            q.append(vi)
        elif d[vi] > d[u] + 1:
            d[vi] = d[u] + 1
            q.append(vi) len(q) len q u = q.popleft() u q.popleft() q.popleft q popleft v = adj[u] v adj[u] adj u for vi in v:
        if d[vi] == -1:
            d[vi] = d[u] + 1
            q.append(vi)
        elif d[vi] > d[u] + 1:
            d[vi] = d[u] + 1
            q.append(vi) vi v if d[vi] == -1:
            d[vi] = d[u] + 1
            q.append(vi)
        elif d[vi] > d[u] + 1:
            d[vi] = d[u] + 1
            q.append(vi) d[vi] == -1 d[vi] d vi -1 d[vi] = d[u] + 1 d[vi] d vi d[u] + 1 d[u] d u 1 q.append(vi) q.append q append vi elif d[vi] > d[u] + 1:
            d[vi] = d[u] + 1
            q.append(vi) d[vi] > d[u] + 1 d[vi] d vi d[u] + 1 d[u] d u 1 d[vi] = d[u] + 1 d[vi] d vi d[u] + 1 d[u] d u 1 q.append(vi) q.append q append vi for i, di in enumerate(d):
    print(i + 1, di) i di enumerate(d) enumerate d print(i + 1, di) print i + 1 i 1 di adj = [list(map(int, input().split()))[2:] for _ in range(n)] [list(map(int, input().split()))[2:] for _ in range(n)] adj n = int(input()) int(input()) n v = adj[u] adj[u] v def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList getSomeInputInt def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE=== def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE=== getSomeInputListInt d = [-1] * n [-1] * n d s = 0 0 s def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList getInputIntList q = collections.deque([]) collections.deque([]) q u = q.popleft() q.popleft() u
import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect

# from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

d = [-1] * n

for a in adj:
    for i in range(len(a)):
        a[i] -= 1

s = 0
q = collections.deque([])
d[s] = 0
q.append(s)
while len(q):
    u = q.popleft()
    v = adj[u]
    for vi in v:
        if d[vi] == -1:
            d[vi] = d[u] + 1
            q.append(vi)
        elif d[vi] > d[u] + 1:
            d[vi] = d[u] + 1
            q.append(vi)

for i, di in enumerate(d):
    print(i + 1, di)

