import sys
input = lambda: sys.stdin.readline().rstrip() 
from collections import deque

def resolve():
    n = int(input())
    ukv = [list(map(int, input().split())) for _ in range(n)]

    dist = [-1]*n
    que = deque()

    que.append(0)
    dist[0] = 0
    while len(que)>0:
        v = que.popleft()
        for i in ukv[v][2:]:
            if dist[i-1]==-1:
                que.append(i-1)
                dist[i-1] = dist[v]+1

    for i in range(n):
        print(i+1, dist[i])

if __name__ == '__main__':
    resolve() import sys sys input = lambda: sys.stdin.readline().rstrip() input lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip from collections import deque def resolve():
    n = int(input())
    ukv = [list(map(int, input().split())) for _ in range(n)]

    dist = [-1]*n
    que = deque()

    que.append(0)
    dist[0] = 0
    while len(que)>0:
        v = que.popleft()
        for i in ukv[v][2:]:
            if dist[i-1]==-1:
                que.append(i-1)
                dist[i-1] = dist[v]+1

    for i in range(n):
        print(i+1, dist[i]) resolve n = int(input()) n int(input()) int input() input list(map(int, input().split())) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ukv = [list(map(int, input().split())) for _ in range(n)] ukv [list(map(int, input().split())) for _ in range(n)] dist = [-1]*n dist [-1]*n [-1] -1 n que = deque() que deque() deque que.append(0) que.append que append 0 dist[0] = 0 dist[0] dist 0 0 while len(que)>0:
        v = que.popleft()
        for i in ukv[v][2:]:
            if dist[i-1]==-1:
                que.append(i-1)
                dist[i-1] = dist[v]+1

     len(que)>0 len(que) len que 0 v = que.popleft() v que.popleft() que.popleft que popleft for i in ukv[v][2:]:
            if dist[i-1]==-1:
                que.append(i-1)
                dist[i-1] = dist[v]+1

     i ukv[v][2:] [v] ukv v 2: 2 if dist[i-1]==-1:
                que.append(i-1)
                dist[i-1] = dist[v]+1

     dist[i-1]==-1 dist[i-1] dist i-1 i 1 -1 que.append(i-1) que.append que append i-1 i 1 dist[i-1] = dist[v]+1 dist[i-1] dist i-1 i 1 dist[v]+1 dist[v] dist 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__':
    resolve() __name__ == '__main__' __name__ '__main__' resolve() resolve def resolve():
    n = int(input())
    ukv = [list(map(int, input().split())) for _ in range(n)]

    dist = [-1]*n
    que = deque()

    que.append(0)
    dist[0] = 0
    while len(que)>0:
        v = que.popleft()
        for i in ukv[v][2:]:
            if dist[i-1]==-1:
                que.append(i-1)
                dist[i-1] = dist[v]+1

    for i in range(n):
        print(i+1, dist[i]) def resolve():
    n = int(input())
    ukv = [list(map(int, input().split())) for _ in range(n)]

    dist = [-1]*n
    que = deque()

    que.append(0)
    dist[0] = 0
    while len(que)>0:
        v = que.popleft()
        for i in ukv[v][2:]:
            if dist[i-1]==-1:
                que.append(i-1)
                dist[i-1] = dist[v]+1

    for i in range(n):
        print(i+1, dist[i]) resolve input = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() input
import sys
input = lambda: sys.stdin.readline().rstrip() 
from collections import deque

def resolve():
    n = int(input())
    ukv = [list(map(int, input().split())) for _ in range(n)]

    dist = [-1]*n
    que = deque()

    que.append(0)
    dist[0] = 0
    while len(que)>0:
        v = que.popleft()
        for i in ukv[v][2:]:
            if dist[i-1]==-1:
                que.append(i-1)
                dist[i-1] = dist[v]+1

    for i in range(n):
        print(i+1, dist[i])

if __name__ == '__main__':
    resolve()
