from collections import deque


def main():
    while que:
        v, d = que.popleft()
        for nv in G[v]:
            nv -= 1
            if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
    for i in range(N):
        print(i+1, D[i])


if __name__ == "__main__":
    N = int(input())
    G = [[] for _ in range(N)]
    for _ in range(N):
        ipts = [int(ipt) for ipt in input().split()]
        G[ipts[0]-1] = ipts[2:]
    D = [-1]*N
    D[0] = 0
    que = deque()
    que.append((0, 0))
    main() from collections import deque def main():
    while que:
        v, d = que.popleft()
        for nv in G[v]:
            nv -= 1
            if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
    for i in range(N):
        print(i+1, D[i]) main while que:
        v, d = que.popleft()
        for nv in G[v]:
            nv -= 1
            if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
     que v, d = que.popleft() v que.popleft() que.popleft que popleft d for nv in G[v]:
            nv -= 1
            if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
     nv G[v] G v nv -= 1 nv 1 if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
     D[nv] == -1 D[nv] D nv -1 D[nv] = D[v] + 1 D[nv] D nv D[v] + 1 D[v] D v 1 que.append((nv, D[nv])) que.append que append (nv, D[nv]) nv D[nv] D nv for i in range(N):
        print(i+1, D[i]) i range(N) range N print(i+1, D[i]) print i+1 i 1 D[i] D i if __name__ == "__main__":
    N = int(input())
    G = [[] for _ in range(N)]
    for _ in range(N):
        ipts = [int(ipt) for ipt in input().split()]
        G[ipts[0]-1] = ipts[2:]
    D = [-1]*N
    D[0] = 0
    que = deque()
    que.append((0, 0))
    main() __name__ == "__main__" __name__ "__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 _ in range(N):
        ipts = [int(ipt) for ipt in input().split()]
        G[ipts[0]-1] = ipts[2:]
     _ range(N) range N int(ipt) for ipt in input().split() for ipt in input().split() ipt input().split() ().split () input split for ipt in input().split() int(ipt) int ipt ipts = [int(ipt) for ipt in input().split()] ipts [int(ipt) for ipt in input().split()] G[ipts[0]-1] = ipts[2:] G[ipts[0]-1] G ipts[0]-1 ipts[0] ipts 0 1 ipts[2:] ipts 2: 2 D = [-1]*N D [-1]*N [-1] -1 N D[0] = 0 D[0] D 0 0 que = deque() que deque() deque que.append((0, 0)) que.append que append (0, 0) 0 0 main() main G = [[] for _ in range(N)] [[] for _ in range(N)] G que = deque() deque() que ipts = [int(ipt) for ipt in input().split()] [int(ipt) for ipt in input().split()] ipts D = [-1]*N [-1]*N D def main():
    while que:
        v, d = que.popleft()
        for nv in G[v]:
            nv -= 1
            if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
    for i in range(N):
        print(i+1, D[i]) def main():
    while que:
        v, d = que.popleft()
        for nv in G[v]:
            nv -= 1
            if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
    for i in range(N):
        print(i+1, D[i]) main N = int(input()) int(input()) N
from collections import deque


def main():
    while que:
        v, d = que.popleft()
        for nv in G[v]:
            nv -= 1
            if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
    for i in range(N):
        print(i+1, D[i])


if __name__ == "__main__":
    N = int(input())
    G = [[] for _ in range(N)]
    for _ in range(N):
        ipts = [int(ipt) for ipt in input().split()]
        G[ipts[0]-1] = ipts[2:]
    D = [-1]*N
    D[0] = 0
    que = deque()
    que.append((0, 0))
    main()
