from collections import deque

def main():
    n = int(input())
    V = [None]*(n+1)
    ans = [None]*(n+1)
    for i in range(1, n+1):
        _, _, *v = map(int, input().split())
        V[i] = v
    q = deque([1])
    ans[1] = 0
    while q:
        node = q.pop()
        d = ans[node]+1
        for v in V[node]:
            if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
    print(1, 0)
    for i, distance in enumerate(ans[2:], 2):
        print(i, distance or -1)

if __name__ == "__main__":
    main() from collections import deque def main():
    n = int(input())
    V = [None]*(n+1)
    ans = [None]*(n+1)
    for i in range(1, n+1):
        _, _, *v = map(int, input().split())
        V[i] = v
    q = deque([1])
    ans[1] = 0
    while q:
        node = q.pop()
        d = ans[node]+1
        for v in V[node]:
            if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
    print(1, 0)
    for i, distance in enumerate(ans[2:], 2):
        print(i, distance or -1) main n = int(input()) n int(input()) int input() input V = [None]*(n+1) V [None]*(n+1) [None] None n+1 n 1 ans = [None]*(n+1) ans [None]*(n+1) [None] None n+1 n 1 for i in range(1, n+1):
        _, _, *v = map(int, input().split())
        V[i] = v
     i range(1, n+1) range 1 n+1 n 1 _, _, *v = map(int, input().split()) _ map(int, input().split()) map int input().split() ().split () input split _ *v v V[i] = v V[i] V i v q = deque([1]) q deque([1]) deque [1] 1 ans[1] = 0 ans[1] ans 1 0 while q:
        node = q.pop()
        d = ans[node]+1
        for v in V[node]:
            if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
     q node = q.pop() node q.pop() q.pop q pop d = ans[node]+1 d ans[node]+1 ans[node] ans node 1 for v in V[node]:
            if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
     v V[node] V node if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
     ans[v] is None ans[v] ans v None ans[v] = d ans[v] ans v d q.appendleft(v) q.appendleft q appendleft v print(1, 0) print 1 0 for i, distance in enumerate(ans[2:], 2):
        print(i, distance or -1) i distance enumerate(ans[2:], 2) enumerate ans[2:] ans 2: 2 2 print(i, distance or -1) print i distance or -1 distance -1 if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    n = int(input())
    V = [None]*(n+1)
    ans = [None]*(n+1)
    for i in range(1, n+1):
        _, _, *v = map(int, input().split())
        V[i] = v
    q = deque([1])
    ans[1] = 0
    while q:
        node = q.pop()
        d = ans[node]+1
        for v in V[node]:
            if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
    print(1, 0)
    for i, distance in enumerate(ans[2:], 2):
        print(i, distance or -1) def main():
    n = int(input())
    V = [None]*(n+1)
    ans = [None]*(n+1)
    for i in range(1, n+1):
        _, _, *v = map(int, input().split())
        V[i] = v
    q = deque([1])
    ans[1] = 0
    while q:
        node = q.pop()
        d = ans[node]+1
        for v in V[node]:
            if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
    print(1, 0)
    for i, distance in enumerate(ans[2:], 2):
        print(i, distance or -1) main
from collections import deque

def main():
    n = int(input())
    V = [None]*(n+1)
    ans = [None]*(n+1)
    for i in range(1, n+1):
        _, _, *v = map(int, input().split())
        V[i] = v
    q = deque([1])
    ans[1] = 0
    while q:
        node = q.pop()
        d = ans[node]+1
        for v in V[node]:
            if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
    print(1, 0)
    for i, distance in enumerate(ans[2:], 2):
        print(i, distance or -1)

if __name__ == "__main__":
    main()
