from collections import deque
n = int(input())
G = []
for _ in range(n):
    u, k, *v = map(int, input().split())
    u -= 1
    G.append(v)
q = deque()
seen = [False] * n
dist = [-1] * n

def main():
    q.append(0)
    seen[0] = True
    dist[0] = 0
    while len(q) > 0:
        now_v = q.popleft()
        for new_v in G[now_v]:
            new_v -= 1
            if seen[new_v] is True:
                continue
            seen[new_v] = True
            q.append(new_v)
#             print(now_v, new_v)
            dist[new_v] = dist[now_v] + 1
    
    for i in range(n):
        print(i+1, dist[i])

if __name__ == '__main__':
    main() from collections import deque n = int(input()) n int(input()) int input() input G = [] G [] for _ in range(n):
    u, k, *v = map(int, input().split())
    u -= 1
    G.append(v) _ range(n) range n u, k, *v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *v v u -= 1 u 1 G.append(v) G.append G append v q = deque() q deque() deque seen = [False] * n seen [False] * n [False] False n dist = [-1] * n dist [-1] * n [-1] -1 n def main():
    q.append(0)
    seen[0] = True
    dist[0] = 0
    while len(q) > 0:
        now_v = q.popleft()
        for new_v in G[now_v]:
            new_v -= 1
            if seen[new_v] is True:
                continue
            seen[new_v] = True
            q.append(new_v)
#             print(now_v, new_v)
            dist[new_v] = dist[now_v] + 1
    
    for i in range(n):
        print(i+1, dist[i]) main q.append(0) q.append q append 0 seen[0] = True seen[0] seen 0 True dist[0] = 0 dist[0] dist 0 0 while len(q) > 0:
        now_v = q.popleft()
        for new_v in G[now_v]:
            new_v -= 1
            if seen[new_v] is True:
                continue
            seen[new_v] = True
            q.append(new_v)
#             print(now_v, new_v)
            dist[new_v] = dist[now_v] + 1
    
     len(q) > 0 len(q) len q 0 now_v = q.popleft() now_v q.popleft() q.popleft q popleft for new_v in G[now_v]:
            new_v -= 1
            if seen[new_v] is True:
                continue
            seen[new_v] = True
            q.append(new_v)
#             print(now_v, new_v)
            dist[new_v] = dist[now_v] + 1
    
     new_v G[now_v] G now_v new_v -= 1 new_v 1 if seen[new_v] is True:
                continue
             seen[new_v] is True seen[new_v] seen new_v True continue seen[new_v] = True seen[new_v] seen new_v True q.append(new_v) q.append q append new_v dist[new_v] = dist[now_v] + 1 dist[new_v] dist new_v dist[now_v] + 1 dist[now_v] dist now_v 1 for i in range(n):
        print(i+1, dist[i]) i range(n) range n print(i+1, dist[i]) print i+1 i 1 dist[i] dist i if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    q.append(0)
    seen[0] = True
    dist[0] = 0
    while len(q) > 0:
        now_v = q.popleft()
        for new_v in G[now_v]:
            new_v -= 1
            if seen[new_v] is True:
                continue
            seen[new_v] = True
            q.append(new_v)
#             print(now_v, new_v)
            dist[new_v] = dist[now_v] + 1
    
    for i in range(n):
        print(i+1, dist[i]) def main():
    q.append(0)
    seen[0] = True
    dist[0] = 0
    while len(q) > 0:
        now_v = q.popleft()
        for new_v in G[now_v]:
            new_v -= 1
            if seen[new_v] is True:
                continue
            seen[new_v] = True
            q.append(new_v)
#             print(now_v, new_v)
            dist[new_v] = dist[now_v] + 1
    
    for i in range(n):
        print(i+1, dist[i]) main n = int(input()) int(input()) n u, k, *v = map(int, input().split()) map(int, input().split()) u G = [] [] G k, *v = map(int, input().split()) map(int, input().split()) k u -= 1 1 u dist = [-1] * n [-1] * n dist q = deque() deque() q seen = [False] * n [False] * n seen
from collections import deque
n = int(input())
G = []
for _ in range(n):
    u, k, *v = map(int, input().split())
    u -= 1
    G.append(v)
q = deque()
seen = [False] * n
dist = [-1] * n

def main():
    q.append(0)
    seen[0] = True
    dist[0] = 0
    while len(q) > 0:
        now_v = q.popleft()
        for new_v in G[now_v]:
            new_v -= 1
            if seen[new_v] is True:
                continue
            seen[new_v] = True
            q.append(new_v)
#             print(now_v, new_v)
            dist[new_v] = dist[now_v] + 1
    
    for i in range(n):
        print(i+1, dist[i])

if __name__ == '__main__':
    main()
