from collections import deque

def main():

    inf = 1000000007
    n = int(input())
    e = [[] for _ in range(n)]
    d = [inf]*n

    for i in range(n):
        m = list(map(int,input().split()))
        for j in range(2,len(m)):
            e[m[0]-1].append(m[j]-1)

    d[0] = 0
    dq = deque([])
    dq.append([0,0])
    while dq:
        c,v = dq.popleft()
        for u in e[v]:
            if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

    for i in range(n):
        if d[i]==inf:print (i+1,-1)
        else        :print (i+1,d[i])

if __name__ == '__main__':
    main()

 from collections import deque def main():

    inf = 1000000007
    n = int(input())
    e = [[] for _ in range(n)]
    d = [inf]*n

    for i in range(n):
        m = list(map(int,input().split()))
        for j in range(2,len(m)):
            e[m[0]-1].append(m[j]-1)

    d[0] = 0
    dq = deque([])
    dq.append([0,0])
    while dq:
        c,v = dq.popleft()
        for u in e[v]:
            if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

    for i in range(n):
        if d[i]==inf:print (i+1,-1)
        else        :print (i+1,d[i]) main inf = 1000000007 inf 1000000007 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) [] e = [[] for _ in range(n)] e [[] for _ in range(n)] d = [inf]*n d [inf]*n [inf] inf n for i in range(n):
        m = list(map(int,input().split()))
        for j in range(2,len(m)):
            e[m[0]-1].append(m[j]-1)

     i range(n) range n m = list(map(int,input().split())) m list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for j in range(2,len(m)):
            e[m[0]-1].append(m[j]-1)

     j range(2,len(m)) range 2 len(m) len m e[m[0]-1].append(m[j]-1) [m[0]-1].append [m[0]-1] e m[0]-1 m[0] m 0 1 append m[j]-1 m[j] m j 1 d[0] = 0 d[0] d 0 0 dq = deque([]) dq deque([]) deque [] dq.append([0,0]) dq.append dq append [0,0] 0 0 while dq:
        c,v = dq.popleft()
        for u in e[v]:
            if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

     dq c,v = dq.popleft() c dq.popleft() dq.popleft dq popleft v for u in e[v]:
            if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

     u e[v] e v if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

     d[u]==inf d[u] d u inf d[u] = c+1 d[u] d u c+1 c 1 dq.append([c+1,u]) dq.append dq append [c+1,u] c+1 c 1 u for i in range(n):
        if d[i]==inf:print (i+1,-1)
        else        :print (i+1,d[i]) i range(n) range n if d[i]==inf:print (i+1,-1)
        else        :print (i+1,d[i]) d[i]==inf d[i] d i inf if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():

    inf = 1000000007
    n = int(input())
    e = [[] for _ in range(n)]
    d = [inf]*n

    for i in range(n):
        m = list(map(int,input().split()))
        for j in range(2,len(m)):
            e[m[0]-1].append(m[j]-1)

    d[0] = 0
    dq = deque([])
    dq.append([0,0])
    while dq:
        c,v = dq.popleft()
        for u in e[v]:
            if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

    for i in range(n):
        if d[i]==inf:print (i+1,-1)
        else        :print (i+1,d[i]) def main():

    inf = 1000000007
    n = int(input())
    e = [[] for _ in range(n)]
    d = [inf]*n

    for i in range(n):
        m = list(map(int,input().split()))
        for j in range(2,len(m)):
            e[m[0]-1].append(m[j]-1)

    d[0] = 0
    dq = deque([])
    dq.append([0,0])
    while dq:
        c,v = dq.popleft()
        for u in e[v]:
            if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

    for i in range(n):
        if d[i]==inf:print (i+1,-1)
        else        :print (i+1,d[i]) main
from collections import deque

def main():

    inf = 1000000007
    n = int(input())
    e = [[] for _ in range(n)]
    d = [inf]*n

    for i in range(n):
        m = list(map(int,input().split()))
        for j in range(2,len(m)):
            e[m[0]-1].append(m[j]-1)

    d[0] = 0
    dq = deque([])
    dq.append([0,0])
    while dq:
        c,v = dq.popleft()
        for u in e[v]:
            if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

    for i in range(n):
        if d[i]==inf:print (i+1,-1)
        else        :print (i+1,d[i])

if __name__ == '__main__':
    main()


