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

d = [-1] * n

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

for i, di in enumerate(d):
    print(i + 1, di) 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 s = 1 s 1 q = [] q [] d[s - 1] = 0 d[s - 1] d s - 1 s 1 0 q.append(s) q.append q append s while len(q):
    u = q.pop()
    v = adj[u - 1]
    for vi in v:
        if d[vi - 1] == -1:
            d[vi - 1] = d[u - 1] + 1         
            q.append(vi)
        elif d[vi - 1] > d[u - 1] + 1:
            d[vi - 1] = d[u - 1] + 1            
            q.append(vi) len(q) len q u = q.pop() u q.pop() q.pop q pop v = adj[u - 1] v adj[u - 1] adj u - 1 u 1 for vi in v:
        if d[vi - 1] == -1:
            d[vi - 1] = d[u - 1] + 1         
            q.append(vi)
        elif d[vi - 1] > d[u - 1] + 1:
            d[vi - 1] = d[u - 1] + 1            
            q.append(vi) vi v if d[vi - 1] == -1:
            d[vi - 1] = d[u - 1] + 1         
            q.append(vi)
        elif d[vi - 1] > d[u - 1] + 1:
            d[vi - 1] = d[u - 1] + 1            
            q.append(vi) d[vi - 1] == -1 d[vi - 1] d vi - 1 vi 1 -1 d[vi - 1] = d[u - 1] + 1 d[vi - 1] d vi - 1 vi 1 d[u - 1] + 1 d[u - 1] d u - 1 u 1 1 q.append(vi) q.append q append vi elif d[vi - 1] > d[u - 1] + 1:
            d[vi - 1] = d[u - 1] + 1            
            q.append(vi) d[vi - 1] > d[u - 1] + 1 d[vi - 1] d vi - 1 vi 1 d[u - 1] + 1 d[u - 1] d u - 1 u 1 1 d[vi - 1] = d[u - 1] + 1 d[vi - 1] d vi - 1 vi 1 d[u - 1] + 1 d[u - 1] d u - 1 u 1 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 q = [] [] q v = adj[u - 1] adj[u - 1] v s = 1 1 s d = [-1] * n [-1] * n d n = int(input()) int(input()) n u = q.pop() q.pop() u 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())
adj = [list(map(int, input().split()))[2:] for _ in range(n)]

d = [-1] * n

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

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