def main():
    import sys
    N = int(sys.stdin.readline())
    L1 = [list(map(int, sys.stdin.readline().split())) for i in range(N)]
    L = [[] for _ in range(N+1)]
    for row in L1:
        L[row[0]] += row[2:]
    #print(L)
    
    d = [-1 for i in range(N+1)] #距離
    def bfs(p, queue=[]):
        queue+=[p]
        d[p]=0
        while queue:
            now = queue.pop(0)
            for ch in L[now]:
                if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
    bfs(1)
    for idx, ans in enumerate(d[1:]):
        print(idx+1, ans)


if __name__=='__main__':
    main() def main():
    import sys
    N = int(sys.stdin.readline())
    L1 = [list(map(int, sys.stdin.readline().split())) for i in range(N)]
    L = [[] for _ in range(N+1)]
    for row in L1:
        L[row[0]] += row[2:]
    #print(L)
    
    d = [-1 for i in range(N+1)] #距離
    def bfs(p, queue=[]):
        queue+=[p]
        d[p]=0
        while queue:
            now = queue.pop(0)
            for ch in L[now]:
                if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
    bfs(1)
    for idx, ans in enumerate(d[1:]):
        print(idx+1, ans) main import sys sys N = int(sys.stdin.readline()) N int(sys.stdin.readline()) int sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline list(map(int, sys.stdin.readline().split())) for i in range(N) for i in range(N) i range(N) range N for i in range(N) list(map(int, sys.stdin.readline().split())) list map(int, sys.stdin.readline().split()) map int sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split L1 = [list(map(int, sys.stdin.readline().split())) for i in range(N)] L1 [list(map(int, sys.stdin.readline().split())) for i in range(N)] [] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [] L = [[] for _ in range(N+1)] L [[] for _ in range(N+1)] for row in L1:
        L[row[0]] += row[2:]
    #print(L)
    
     row L1 L[row[0]] += row[2:] L[row[0]] L row[0] row 0 row[2:] row 2: 2 -1 for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) -1 d = [-1 for i in range(N+1)] d [-1 for i in range(N+1)] def bfs(p, queue=[]):
        queue+=[p]
        d[p]=0
        while queue:
            now = queue.pop(0)
            for ch in L[now]:
                if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
     bfs queue+=[p] queue [p] p d[p]=0 d[p] d p 0 while queue:
            now = queue.pop(0)
            for ch in L[now]:
                if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
     queue now = queue.pop(0) now queue.pop(0) queue.pop queue pop 0 for ch in L[now]:
                if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
     ch L[now] L now if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
     d[ch] ==-1 d[ch] d ch -1 d[ch] = d[now]+1 d[ch] d ch d[now]+1 d[now] d now 1 queue +=[ch] queue [ch] ch p p queue=[] queue [] bfs(1) bfs 1 for idx, ans in enumerate(d[1:]):
        print(idx+1, ans) idx ans enumerate(d[1:]) enumerate d[1:] d 1: 1 print(idx+1, ans) print idx+1 idx 1 ans if __name__=='__main__':
    main() __name__=='__main__' __name__ '__main__' main() main def main():
    import sys
    N = int(sys.stdin.readline())
    L1 = [list(map(int, sys.stdin.readline().split())) for i in range(N)]
    L = [[] for _ in range(N+1)]
    for row in L1:
        L[row[0]] += row[2:]
    #print(L)
    
    d = [-1 for i in range(N+1)] #距離
    def bfs(p, queue=[]):
        queue+=[p]
        d[p]=0
        while queue:
            now = queue.pop(0)
            for ch in L[now]:
                if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
    bfs(1)
    for idx, ans in enumerate(d[1:]):
        print(idx+1, ans) def main():
    import sys
    N = int(sys.stdin.readline())
    L1 = [list(map(int, sys.stdin.readline().split())) for i in range(N)]
    L = [[] for _ in range(N+1)]
    for row in L1:
        L[row[0]] += row[2:]
    #print(L)
    
    d = [-1 for i in range(N+1)] #距離
    def bfs(p, queue=[]):
        queue+=[p]
        d[p]=0
        while queue:
            now = queue.pop(0)
            for ch in L[now]:
                if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
    bfs(1)
    for idx, ans in enumerate(d[1:]):
        print(idx+1, ans) main
def main():
    import sys
    N = int(sys.stdin.readline())
    L1 = [list(map(int, sys.stdin.readline().split())) for i in range(N)]
    L = [[] for _ in range(N+1)]
    for row in L1:
        L[row[0]] += row[2:]
    #print(L)
    
    d = [-1 for i in range(N+1)] #距離
    def bfs(p, queue=[]):
        queue+=[p]
        d[p]=0
        while queue:
            now = queue.pop(0)
            for ch in L[now]:
                if d[ch] ==-1: #未開
                    d[ch] = d[now]+1
                    queue +=[ch]
    
    
    bfs(1)
    for idx, ans in enumerate(d[1:]):
        print(idx+1, ans)


if __name__=='__main__':
    main()
