
n = int(input())
X = []
for _ in range(n):
    x = list(map(int, input().split()))
    if x[1] == 0:
        X.append([])
    else:
        X.append(x[2:])
Y = [-1 for _ in range(n)]
def search(i = 0, d = 0):
    if Y[i] > d or Y[i] < 0:
        Y[i] = d
        for x in X[i]:
            search(x - 1, d + 1)
    
search()

for i, y in enumerate(Y):
    print('{} {}'.format(i + 1, y))    
 n = int(input()) n int(input()) int input() input X = [] X [] for _ in range(n):
    x = list(map(int, input().split()))
    if x[1] == 0:
        X.append([])
    else:
        X.append(x[2:]) _ range(n) range n x = list(map(int, input().split())) x list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split if x[1] == 0:
        X.append([])
    else:
        X.append(x[2:]) x[1] == 0 x[1] x 1 0 X.append([]) X.append X append [] X.append(x[2:]) X.append X append x[2:] x 2: 2 -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 Y = [-1 for _ in range(n)] Y [-1 for _ in range(n)] def search(i = 0, d = 0):
    if Y[i] > d or Y[i] < 0:
        Y[i] = d
        for x in X[i]:
            search(x - 1, d + 1)
     search if Y[i] > d or Y[i] < 0:
        Y[i] = d
        for x in X[i]:
            search(x - 1, d + 1)
     Y[i] > d or Y[i] < 0 Y[i] > d Y[i] Y i d Y[i] < 0 Y[i] Y i 0 Y[i] = d Y[i] Y i d for x in X[i]:
            search(x - 1, d + 1)
     x X[i] X i search(x - 1, d + 1) search x - 1 x 1 d + 1 d 1 i = 0 i 0 d = 0 d 0 search() search for i, y in enumerate(Y):
    print('{} {}'.format(i + 1, y))     i y enumerate(Y) enumerate Y print('{} {}'.format(i + 1, y)) print '{} {}'.format(i + 1, y) '{} {}'.format '{} {}' format i + 1 i 1 y x = list(map(int, input().split())) list(map(int, input().split())) x Y = [-1 for _ in range(n)] [-1 for _ in range(n)] Y X = [] [] X n = int(input()) int(input()) n def search(i = 0, d = 0):
    if Y[i] > d or Y[i] < 0:
        Y[i] = d
        for x in X[i]:
            search(x - 1, d + 1)
     def search(i = 0, d = 0):
    if Y[i] > d or Y[i] < 0:
        Y[i] = d
        for x in X[i]:
            search(x - 1, d + 1)
     search

n = int(input())
X = []
for _ in range(n):
    x = list(map(int, input().split()))
    if x[1] == 0:
        X.append([])
    else:
        X.append(x[2:])
Y = [-1 for _ in range(n)]
def search(i = 0, d = 0):
    if Y[i] > d or Y[i] < 0:
        Y[i] = d
        for x in X[i]:
            search(x - 1, d + 1)
    
search()

for i, y in enumerate(Y):
    print('{} {}'.format(i + 1, y))    

