from collections import deque
import sys
read = sys.stdin.read
readline = sys.stdin.readline


def main():
    n = int(readline())

    E = [[]]
    for _ in range(n):
        u,k,*v = map(int, readline().split())
        E.append(v)

    d = [-1] * (n+1)
    
    queue = deque([1])
    d[1] = 0
    while queue:
        v = queue.popleft()
        for e in E[v]:
            if d[e] == -1:
                d[e] = d[v] + 1
                queue.append(e)

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


if __name__ == "__main__":
    main() from collections import deque import sys sys read = sys.stdin.read read sys.stdin.read sys.stdin sys stdin read readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline def main():
    n = int(readline())

    E = [[]]
    for _ in range(n):
        u,k,*v = map(int, readline().split())
        E.append(v)

    d = [-1] * (n+1)
    
    queue = deque([1])
    d[1] = 0
    while queue:
        v = queue.popleft()
        for e in E[v]:
            if d[e] == -1:
                d[e] = d[v] + 1
                queue.append(e)

    for i in range(1, n+1):
        print(i, d[i]) main n = int(readline()) n int(readline()) int readline() readline E = [[]] E [[]] [] for _ in range(n):
        u,k,*v = map(int, readline().split())
        E.append(v)

     _ range(n) range n u,k,*v = map(int, readline().split()) u map(int, readline().split()) map int readline().split() ().split () readline split k *v v E.append(v) E.append E append v d = [-1] * (n+1) d [-1] * (n+1) [-1] -1 n+1 n 1 queue = deque([1]) queue deque([1]) deque [1] 1 d[1] = 0 d[1] d 1 0 while queue:
        v = queue.popleft()
        for e in E[v]:
            if d[e] == -1:
                d[e] = d[v] + 1
                queue.append(e)

     queue v = queue.popleft() v queue.popleft() queue.popleft queue popleft for e in E[v]:
            if d[e] == -1:
                d[e] = d[v] + 1
                queue.append(e)

     e E[v] E v if d[e] == -1:
                d[e] = d[v] + 1
                queue.append(e)

     d[e] == -1 d[e] d e -1 d[e] = d[v] + 1 d[e] d e d[v] + 1 d[v] d v 1 queue.append(e) queue.append queue append e for i in range(1, n+1):
        print(i, d[i]) i range(1, n+1) range 1 n+1 n 1 print(i, d[i]) print i d[i] d i if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main read = sys.stdin.read sys.stdin.read read readline = sys.stdin.readline sys.stdin.readline readline def main():
    n = int(readline())

    E = [[]]
    for _ in range(n):
        u,k,*v = map(int, readline().split())
        E.append(v)

    d = [-1] * (n+1)
    
    queue = deque([1])
    d[1] = 0
    while queue:
        v = queue.popleft()
        for e in E[v]:
            if d[e] == -1:
                d[e] = d[v] + 1
                queue.append(e)

    for i in range(1, n+1):
        print(i, d[i]) def main():
    n = int(readline())

    E = [[]]
    for _ in range(n):
        u,k,*v = map(int, readline().split())
        E.append(v)

    d = [-1] * (n+1)
    
    queue = deque([1])
    d[1] = 0
    while queue:
        v = queue.popleft()
        for e in E[v]:
            if d[e] == -1:
                d[e] = d[v] + 1
                queue.append(e)

    for i in range(1, n+1):
        print(i, d[i]) main
from collections import deque
import sys
read = sys.stdin.read
readline = sys.stdin.readline


def main():
    n = int(readline())

    E = [[]]
    for _ in range(n):
        u,k,*v = map(int, readline().split())
        E.append(v)

    d = [-1] * (n+1)
    
    queue = deque([1])
    d[1] = 0
    while queue:
        v = queue.popleft()
        for e in E[v]:
            if d[e] == -1:
                d[e] = d[v] + 1
                queue.append(e)

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


if __name__ == "__main__":
    main()
