import sys
import queue
input = sys.stdin.readline
n = int(input())
g = [[] for _ in range(n+1)]
d = [-1 for _ in range(n+1)]
for _ in range(n):
    c = list(map(int,input().split()))
    g[c[0]] = c[2:]

depth = 0
q = queue.Queue()
p = queue.Queue()
q.put(1)
while not q.empty():
    while not q.empty():
        s = q.get()
        if d[s] < 0:
            d[s] = depth
            for i in g[s]:
                p.put(i)
    p, q = q, p
    depth += 1

for i in range(1,n+1):
    print (i,d[i]) import sys sys import queue queue input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline n = int(input()) n int(input()) int input() input [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] g = [[] for _ in range(n+1)] g [[] for _ in range(n+1)] -1 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) -1 d = [-1 for _ in range(n+1)] d [-1 for _ in range(n+1)] for _ in range(n):
    c = list(map(int,input().split()))
    g[c[0]] = c[2:] _ range(n) range n c = list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split g[c[0]] = c[2:] g[c[0]] g c[0] c 0 c[2:] c 2: 2 depth = 0 depth 0 q = queue.Queue() q queue.Queue() queue.Queue queue Queue p = queue.Queue() p queue.Queue() queue.Queue queue Queue q.put(1) q.put q put 1 while not q.empty():
    while not q.empty():
        s = q.get()
        if d[s] < 0:
            d[s] = depth
            for i in g[s]:
                p.put(i)
    p, q = q, p
    depth += 1 not q.empty() q.empty() q.empty q empty while not q.empty():
        s = q.get()
        if d[s] < 0:
            d[s] = depth
            for i in g[s]:
                p.put(i)
     not q.empty() q.empty() q.empty q empty s = q.get() s q.get() q.get q get if d[s] < 0:
            d[s] = depth
            for i in g[s]:
                p.put(i)
     d[s] < 0 d[s] d s 0 d[s] = depth d[s] d s depth for i in g[s]:
                p.put(i)
     i g[s] g s p.put(i) p.put p put i p, q = q, p p q q p depth += 1 depth 1 for i in range(1,n+1):
    print (i,d[i]) i range(1,n+1) range 1 n+1 n 1 print (i,d[i]) print i d[i] d i q = q, p p q g = [[] for _ in range(n+1)] [[] for _ in range(n+1)] g depth = 0 0 depth p = queue.Queue() queue.Queue() p p, q = q, p q p s = q.get() q.get() s input = sys.stdin.readline sys.stdin.readline input depth += 1 1 depth c = list(map(int,input().split())) list(map(int,input().split())) c q = queue.Queue() queue.Queue() q n = int(input()) int(input()) n d = [-1 for _ in range(n+1)] [-1 for _ in range(n+1)] d
import sys
import queue
input = sys.stdin.readline
n = int(input())
g = [[] for _ in range(n+1)]
d = [-1 for _ in range(n+1)]
for _ in range(n):
    c = list(map(int,input().split()))
    g[c[0]] = c[2:]

depth = 0
q = queue.Queue()
p = queue.Queue()
q.put(1)
while not q.empty():
    while not q.empty():
        s = q.get()
        if d[s] < 0:
            d[s] = depth
            for i in g[s]:
                p.put(i)
    p, q = q, p
    depth += 1

for i in range(1,n+1):
    print (i,d[i])
