cin = open(0).read().strip().split('\n')
n = int(cin[0])
v = [list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:]]

import queue
q = queue.Queue()
q.put(0)

dist = [-1]*n
dist[0] = 0
seen = [False]*n
while not q.empty():
    num = q.get()
    seen[num] = True
    for a in v[num]:
        if dist[a] != -1: continue
        dist[a] = dist[num] + 1
        q.put(a)

for idx, num in enumerate(dist):
    print(idx+1, num) cin = open(0).read().strip().split('\n') cin open(0).read().strip().split('\n') (0).read().strip().split (0).read().strip() (0).read().strip (0).read() (0).read (0) open 0 read strip split '\n' n = int(cin[0]) n int(cin[0]) int cin[0] cin 0 list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:] for a in cin[1:] a cin[1:] cin 1: 1 for a in cin[1:] list(map(lambda x: int(x)-1, a.split(' ')[2:])) list map(lambda x: int(x)-1, a.split(' ')[2:]) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x a.split(' ')[2:] a.split(' ') a.split a split ' ' 2: 2 v = [list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:]] v [list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:]] import queue queue q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(0) q.put q put 0 dist = [-1]*n dist [-1]*n [-1] -1 n dist[0] = 0 dist[0] dist 0 0 seen = [False]*n seen [False]*n [False] False n while not q.empty():
    num = q.get()
    seen[num] = True
    for a in v[num]:
        if dist[a] != -1: continue
        dist[a] = dist[num] + 1
        q.put(a) not q.empty() q.empty() q.empty q empty num = q.get() num q.get() q.get q get seen[num] = True seen[num] seen num True for a in v[num]:
        if dist[a] != -1: continue
        dist[a] = dist[num] + 1
        q.put(a) a v[num] v num if dist[a] != -1: continue
         dist[a] != -1 dist[a] dist a -1 dist[a] = dist[num] + 1 dist[a] dist a dist[num] + 1 dist[num] dist num 1 q.put(a) q.put q put a for idx, num in enumerate(dist):
    print(idx+1, num) idx num enumerate(dist) enumerate dist print(idx+1, num) print idx+1 idx 1 num q = queue.Queue() queue.Queue() q num = q.get() q.get() num v = [list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:]] [list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:]] v seen = [False]*n [False]*n seen cin = open(0).read().strip().split('\n') open(0).read().strip().split('\n') cin dist = [-1]*n [-1]*n dist n = int(cin[0]) int(cin[0]) n
cin = open(0).read().strip().split('\n')
n = int(cin[0])
v = [list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:]]

import queue
q = queue.Queue()
q.put(0)

dist = [-1]*n
dist[0] = 0
seen = [False]*n
while not q.empty():
    num = q.get()
    seen[num] = True
    for a in v[num]:
        if dist[a] != -1: continue
        dist[a] = dist[num] + 1
        q.put(a)

for idx, num in enumerate(dist):
    print(idx+1, num)
