n = int(input())
G = [[] for _ in range(n)]
for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
NIL = 100
Q = [[1, 0]]
F = []
D = [NIL for _ in range(n)]
while Q != []:
    u, d = Q.pop(0)
    if d < D[u-1]:
        D[u-1] = d
    if u not in F:
        F.append(u)
    for v in G[u-1]:
        if v not in F:
            Q.append([v, d+1])
'''
for ni in range(1, n):
    Q = [[ni+1, 0]]
    F = []
    while Q != []:
        u, d = Q.pop(0)
        if u == 1 and d < D[ni]:
            D[ni] = d
        if u not in F:
            F.append(u)
        for v in G[u-1]:
            if v not in F:
                Q.append([v, d+1])
'''
for ni in range(n):
    if D[ni] == NIL:
        print(ni+1, -1)
    else:
        print(ni+1, D[ni]) n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] G = [[] for _ in range(n)] G [[] for _ in range(n)] for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v _ range(n) range n 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 u, k, *v = [int(i) for i in input().split()] u [int(i) for i in input().split()] k *v v G[u - 1] = v G[u - 1] G u - 1 u 1 v NIL = 100 NIL 100 Q = [[1, 0]] Q [[1, 0]] [1, 0] 1 0 F = [] F [] NIL for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) NIL D = [NIL for _ in range(n)] D [NIL for _ in range(n)] while Q != []:
    u, d = Q.pop(0)
    if d < D[u-1]:
        D[u-1] = d
    if u not in F:
        F.append(u)
    for v in G[u-1]:
        if v not in F:
            Q.append([v, d+1]) Q != [] Q [] u, d = Q.pop(0) u Q.pop(0) Q.pop Q pop 0 d if d < D[u-1]:
        D[u-1] = d
     d < D[u-1] d D[u-1] D u-1 u 1 D[u-1] = d D[u-1] D u-1 u 1 d if u not in F:
        F.append(u)
     u not in F u F F.append(u) F.append F append u for v in G[u-1]:
        if v not in F:
            Q.append([v, d+1]) v G[u-1] G u-1 u 1 if v not in F:
            Q.append([v, d+1]) v not in F v F Q.append([v, d+1]) Q.append Q append [v, d+1] v d+1 d 1 '''
for ni in range(1, n):
    Q = [[ni+1, 0]]
    F = []
    while Q != []:
        u, d = Q.pop(0)
        if u == 1 and d < D[ni]:
            D[ni] = d
        if u not in F:
            F.append(u)
        for v in G[u-1]:
            if v not in F:
                Q.append([v, d+1])
''' for ni in range(n):
    if D[ni] == NIL:
        print(ni+1, -1)
    else:
        print(ni+1, D[ni]) ni range(n) range n if D[ni] == NIL:
        print(ni+1, -1)
    else:
        print(ni+1, D[ni]) D[ni] == NIL D[ni] D ni NIL print(ni+1, -1) print ni+1 ni 1 -1 print(ni+1, D[ni]) print ni+1 ni 1 D[ni] D ni NIL = 100 100 NIL k, *v = [int(i) for i in input().split()] [int(i) for i in input().split()] k Q = [[1, 0]] [[1, 0]] Q u, d = Q.pop(0) Q.pop(0) u d = Q.pop(0) Q.pop(0) d F = [] [] F u, k, *v = [int(i) for i in input().split()] [int(i) for i in input().split()] u n = int(input()) int(input()) n D = [NIL for _ in range(n)] [NIL for _ in range(n)] D G = [[] for _ in range(n)] [[] for _ in range(n)] G
n = int(input())
G = [[] for _ in range(n)]
for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
NIL = 100
Q = [[1, 0]]
F = []
D = [NIL for _ in range(n)]
while Q != []:
    u, d = Q.pop(0)
    if d < D[u-1]:
        D[u-1] = d
    if u not in F:
        F.append(u)
    for v in G[u-1]:
        if v not in F:
            Q.append([v, d+1])
'''
for ni in range(1, n):
    Q = [[ni+1, 0]]
    F = []
    while Q != []:
        u, d = Q.pop(0)
        if u == 1 and d < D[ni]:
            D[ni] = d
        if u not in F:
            F.append(u)
        for v in G[u-1]:
            if v not in F:
                Q.append([v, d+1])
'''
for ni in range(n):
    if D[ni] == NIL:
        print(ni+1, -1)
    else:
        print(ni+1, D[ni])
