import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from collections import deque,defaultdict,Counter
def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N):
        A = list(map(int,input().split()))
        for a in A[2:]:
            a -= 1
            G[i].append(a)
    
    q = deque([0])
    dist = [-1] * N
    dist[0] = 0
    while q:
        v = q.popleft()
        for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
    ans = [(i + 1,dist[i]) for i in range(N)]
    for t in ans:
        print(*t)
if __name__ == '__main__':
    main() import sys sys sys.setrecursionlimit(10000000) sys.setrecursionlimit sys setrecursionlimit 10000000 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 INF = 10 ** 15 INF 10 ** 15 10 15 from collections import deque,defaultdict,Counter def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N):
        A = list(map(int,input().split()))
        for a in A[2:]:
            a -= 1
            G[i].append(a)
    
    q = deque([0])
    dist = [-1] * N
    dist[0] = 0
    while q:
        v = q.popleft()
        for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
    ans = [(i + 1,dist[i]) for i in range(N)]
    for t in ans:
        print(*t) main 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 i in range(N):
        A = list(map(int,input().split()))
        for a in A[2:]:
            a -= 1
            G[i].append(a)
    
     i range(N) range N A = list(map(int,input().split())) A list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for a in A[2:]:
            a -= 1
            G[i].append(a)
    
     a A[2:] A 2: 2 a -= 1 a 1 G[i].append(a) [i].append [i] G i append a q = deque([0]) q deque([0]) deque [0] 0 dist = [-1] * N dist [-1] * N [-1] -1 N dist[0] = 0 dist[0] dist 0 0 while q:
        v = q.popleft()
        for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
     q v = q.popleft() v q.popleft() q.popleft q popleft for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
     e G[v] G v if dist[e] >= 0:
                continue
             dist[e] >= 0 dist[e] dist e 0 continue dist[e] = dist[v] + 1 dist[e] dist e dist[v] + 1 dist[v] dist v 1 q.append(e) q.append q append e (i + 1,dist[i]) for i in range(N) for i in range(N) i range(N) range N for i in range(N) (i + 1,dist[i]) i + 1 i 1 dist[i] dist i ans = [(i + 1,dist[i]) for i in range(N)] ans [(i + 1,dist[i]) for i in range(N)] for t in ans:
        print(*t) t ans print(*t) print *t t if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N):
        A = list(map(int,input().split()))
        for a in A[2:]:
            a -= 1
            G[i].append(a)
    
    q = deque([0])
    dist = [-1] * N
    dist[0] = 0
    while q:
        v = q.popleft()
        for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
    ans = [(i + 1,dist[i]) for i in range(N)]
    for t in ans:
        print(*t) def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N):
        A = list(map(int,input().split()))
        for a in A[2:]:
            a -= 1
            G[i].append(a)
    
    q = deque([0])
    dist = [-1] * N
    dist[0] = 0
    while q:
        v = q.popleft()
        for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
    ans = [(i + 1,dist[i]) for i in range(N)]
    for t in ans:
        print(*t) main INF = 10 ** 15 10 ** 15 INF MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from collections import deque,defaultdict,Counter
def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N):
        A = list(map(int,input().split()))
        for a in A[2:]:
            a -= 1
            G[i].append(a)
    
    q = deque([0])
    dist = [-1] * N
    dist[0] = 0
    while q:
        v = q.popleft()
        for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
    ans = [(i + 1,dist[i]) for i in range(N)]
    for t in ans:
        print(*t)
if __name__ == '__main__':
    main()
