# -*- coding: utf-8 -*-
'''
所要時間は、分位であった。
'''


# ライブラリのインポート
#import re #正規表現
import sys
#import heapq
#import bisect
from collections import deque
#import math

def main():
    inf = 10000
    n = int(input())
    D = [inf for i in range(n)]
    L = [[] for i in range(n)]
    for i, line in enumerate(sys.stdin):
        L[i] = list(map(lambda x: int(x)-1, list(line.strip("\n").split())))

    q = deque([0])
    D[0] = 0
    while(len(q)):
        u = q.popleft()
        if L[u][1] == -1:
            continue
        for j in range(2,len(L[u])):
            if D[L[u][j]] == inf:
                q.append(L[u][j])
            D[L[u][j]] = min(D[L[u][0]] + 1, D[L[u][j]]) 

                      
    for i in range(n):
        print("{} {}".format(i+1,D[i])) if D[i] < inf else print("{} {}".format(i+1,-1))
            
if __name__ == '__main__':
    main()
 '''
所要時間は、分位であった。
''' import sys sys from collections import deque def main():
    inf = 10000
    n = int(input())
    D = [inf for i in range(n)]
    L = [[] for i in range(n)]
    for i, line in enumerate(sys.stdin):
        L[i] = list(map(lambda x: int(x)-1, list(line.strip("\n").split())))

    q = deque([0])
    D[0] = 0
    while(len(q)):
        u = q.popleft()
        if L[u][1] == -1:
            continue
        for j in range(2,len(L[u])):
            if D[L[u][j]] == inf:
                q.append(L[u][j])
            D[L[u][j]] = min(D[L[u][0]] + 1, D[L[u][j]]) 

                      
    for i in range(n):
        print("{} {}".format(i+1,D[i])) if D[i] < inf else print("{} {}".format(i+1,-1))
             main inf = 10000 inf 10000 n = int(input()) n int(input()) int input() input inf for i in range(n) for i in range(n) i range(n) range n for i in range(n) inf D = [inf for i in range(n)] D [inf for i in range(n)] [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] L = [[] for i in range(n)] L [[] for i in range(n)] for i, line in enumerate(sys.stdin):
        L[i] = list(map(lambda x: int(x)-1, list(line.strip("\n").split())))

     i line enumerate(sys.stdin) enumerate sys.stdin sys stdin L[i] = list(map(lambda x: int(x)-1, list(line.strip("\n").split()))) L[i] L i list(map(lambda x: int(x)-1, list(line.strip("\n").split()))) list map(lambda x: int(x)-1, list(line.strip("\n").split())) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x list(line.strip("\n").split()) list line.strip("\n").split() line.strip("\n").split line.strip("\n") line.strip line strip "\n" split q = deque([0]) q deque([0]) deque [0] 0 D[0] = 0 D[0] D 0 0 while(len(q)):
        u = q.popleft()
        if L[u][1] == -1:
            continue
        for j in range(2,len(L[u])):
            if D[L[u][j]] == inf:
                q.append(L[u][j])
            D[L[u][j]] = min(D[L[u][0]] + 1, D[L[u][j]]) 

                      
     len(q) len q u = q.popleft() u q.popleft() q.popleft q popleft if L[u][1] == -1:
            continue
         L[u][1] == -1 L[u][1] [u] L u 1 -1 continue for j in range(2,len(L[u])):
            if D[L[u][j]] == inf:
                q.append(L[u][j])
            D[L[u][j]] = min(D[L[u][0]] + 1, D[L[u][j]]) 

                      
     j range(2,len(L[u])) range 2 len(L[u]) len L[u] L u if D[L[u][j]] == inf:
                q.append(L[u][j])
             D[L[u][j]] == inf D[L[u][j]] D L[u][j] [u] L u j inf q.append(L[u][j]) q.append q append L[u][j] [u] L u j D[L[u][j]] = min(D[L[u][0]] + 1, D[L[u][j]]) D[L[u][j]] D L[u][j] [u] L u j min(D[L[u][0]] + 1, D[L[u][j]]) min D[L[u][0]] + 1 D[L[u][0]] D L[u][0] [u] L u 0 1 D[L[u][j]] D L[u][j] [u] L u j for i in range(n):
        print("{} {}".format(i+1,D[i])) if D[i] < inf else print("{} {}".format(i+1,-1))
             i range(n) range n print("{} {}".format(i+1,D[i])) if D[i] < inf else print("{} {}".format(i+1,-1)) D[i] < inf D[i] D i inf print("{} {}".format(i+1,D[i])) print "{} {}".format(i+1,D[i]) "{} {}".format "{} {}" format i+1 i 1 D[i] D i print("{} {}".format(i+1,-1)) print "{} {}".format(i+1,-1) "{} {}".format "{} {}" format i+1 i 1 -1 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    inf = 10000
    n = int(input())
    D = [inf for i in range(n)]
    L = [[] for i in range(n)]
    for i, line in enumerate(sys.stdin):
        L[i] = list(map(lambda x: int(x)-1, list(line.strip("\n").split())))

    q = deque([0])
    D[0] = 0
    while(len(q)):
        u = q.popleft()
        if L[u][1] == -1:
            continue
        for j in range(2,len(L[u])):
            if D[L[u][j]] == inf:
                q.append(L[u][j])
            D[L[u][j]] = min(D[L[u][0]] + 1, D[L[u][j]]) 

                      
    for i in range(n):
        print("{} {}".format(i+1,D[i])) if D[i] < inf else print("{} {}".format(i+1,-1))
             def main():
    inf = 10000
    n = int(input())
    D = [inf for i in range(n)]
    L = [[] for i in range(n)]
    for i, line in enumerate(sys.stdin):
        L[i] = list(map(lambda x: int(x)-1, list(line.strip("\n").split())))

    q = deque([0])
    D[0] = 0
    while(len(q)):
        u = q.popleft()
        if L[u][1] == -1:
            continue
        for j in range(2,len(L[u])):
            if D[L[u][j]] == inf:
                q.append(L[u][j])
            D[L[u][j]] = min(D[L[u][0]] + 1, D[L[u][j]]) 

                      
    for i in range(n):
        print("{} {}".format(i+1,D[i])) if D[i] < inf else print("{} {}".format(i+1,-1))
             main
# -*- coding: utf-8 -*-
'''
所要時間は、分位であった。
'''


# ライブラリのインポート
#import re #正規表現
import sys
#import heapq
#import bisect
from collections import deque
#import math

def main():
    inf = 10000
    n = int(input())
    D = [inf for i in range(n)]
    L = [[] for i in range(n)]
    for i, line in enumerate(sys.stdin):
        L[i] = list(map(lambda x: int(x)-1, list(line.strip("\n").split())))

    q = deque([0])
    D[0] = 0
    while(len(q)):
        u = q.popleft()
        if L[u][1] == -1:
            continue
        for j in range(2,len(L[u])):
            if D[L[u][j]] == inf:
                q.append(L[u][j])
            D[L[u][j]] = min(D[L[u][0]] + 1, D[L[u][j]]) 

                      
    for i in range(n):
        print("{} {}".format(i+1,D[i])) if D[i] < inf else print("{} {}".format(i+1,-1))
            
if __name__ == '__main__':
    main()

