# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: print('Yes' if b else 'No')
YESNO=lambda b: print('YES' if b else 'NO')

def main():
    N=int(input())
    edge=[[] for _ in range(N)]
    for i in range(N):
        l=list(map(lambda x:int(x)-1,input().split()))
        edge[i]=l[2:]
    d=[INF]*N
    d[0]=0
    que=deque()
    que.append(0)
    while que:
        v=que.popleft()
        for nv in edge[v]:
            if d[nv]>d[v]+1:
                d[nv]=d[v]+1
                que.append(nv)
    for i in range(N):
        print(i+1,d[i] if d[i]!=INF else -1)

if __name__ == '__main__':
    main()
 import sys sys from collections import deque sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 INF=10**18 INF 10**18 10 18 MOD=10**9+7 MOD 10**9+7 10**9 10 9 7 input=lambda: sys.stdin.readline().rstrip() input lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip YesNo=lambda b: print('Yes' if b else 'No') YesNo lambda b: print('Yes' if b else 'No') print('Yes' if b else 'No') print 'Yes' if b else 'No' b 'Yes' 'No' b YESNO=lambda b: print('YES' if b else 'NO') YESNO lambda b: print('YES' if b else 'NO') print('YES' if b else 'NO') print 'YES' if b else 'NO' b 'YES' 'NO' b def main():
    N=int(input())
    edge=[[] for _ in range(N)]
    for i in range(N):
        l=list(map(lambda x:int(x)-1,input().split()))
        edge[i]=l[2:]
    d=[INF]*N
    d[0]=0
    que=deque()
    que.append(0)
    while que:
        v=que.popleft()
        for nv in edge[v]:
            if d[nv]>d[v]+1:
                d[nv]=d[v]+1
                que.append(nv)
    for i in range(N):
        print(i+1,d[i] if d[i]!=INF else -1) 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) [] edge=[[] for _ in range(N)] edge [[] for _ in range(N)] for i in range(N):
        l=list(map(lambda x:int(x)-1,input().split()))
        edge[i]=l[2:]
     i range(N) range N l=list(map(lambda x:int(x)-1,input().split())) l list(map(lambda x:int(x)-1,input().split())) list map(lambda x:int(x)-1,input().split()) map lambda x:int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split edge[i]=l[2:] edge[i] edge i l[2:] l 2: 2 d=[INF]*N d [INF]*N [INF] INF N d[0]=0 d[0] d 0 0 que=deque() que deque() deque que.append(0) que.append que append 0 while que:
        v=que.popleft()
        for nv in edge[v]:
            if d[nv]>d[v]+1:
                d[nv]=d[v]+1
                que.append(nv)
     que v=que.popleft() v que.popleft() que.popleft que popleft for nv in edge[v]:
            if d[nv]>d[v]+1:
                d[nv]=d[v]+1
                que.append(nv)
     nv edge[v] edge v if d[nv]>d[v]+1:
                d[nv]=d[v]+1
                que.append(nv)
     d[nv]>d[v]+1 d[nv] d nv d[v]+1 d[v] d v 1 d[nv]=d[v]+1 d[nv] d nv d[v]+1 d[v] d v 1 que.append(nv) que.append que append nv for i in range(N):
        print(i+1,d[i] if d[i]!=INF else -1) i range(N) range N print(i+1,d[i] if d[i]!=INF else -1) print i+1 i 1 d[i] if d[i]!=INF else -1 d[i]!=INF d[i] d i INF d[i] d i -1 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main INF=10**18 10**18 INF MOD=10**9+7 10**9+7 MOD YESNO=lambda b: print('YES' if b else 'NO') lambda b: print('YES' if b else 'NO') YESNO YesNo=lambda b: print('Yes' if b else 'No') lambda b: print('Yes' if b else 'No') YesNo input=lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() input def main():
    N=int(input())
    edge=[[] for _ in range(N)]
    for i in range(N):
        l=list(map(lambda x:int(x)-1,input().split()))
        edge[i]=l[2:]
    d=[INF]*N
    d[0]=0
    que=deque()
    que.append(0)
    while que:
        v=que.popleft()
        for nv in edge[v]:
            if d[nv]>d[v]+1:
                d[nv]=d[v]+1
                que.append(nv)
    for i in range(N):
        print(i+1,d[i] if d[i]!=INF else -1) def main():
    N=int(input())
    edge=[[] for _ in range(N)]
    for i in range(N):
        l=list(map(lambda x:int(x)-1,input().split()))
        edge[i]=l[2:]
    d=[INF]*N
    d[0]=0
    que=deque()
    que.append(0)
    while que:
        v=que.popleft()
        for nv in edge[v]:
            if d[nv]>d[v]+1:
                d[nv]=d[v]+1
                que.append(nv)
    for i in range(N):
        print(i+1,d[i] if d[i]!=INF else -1) main
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: print('Yes' if b else 'No')
YESNO=lambda b: print('YES' if b else 'NO')

def main():
    N=int(input())
    edge=[[] for _ in range(N)]
    for i in range(N):
        l=list(map(lambda x:int(x)-1,input().split()))
        edge[i]=l[2:]
    d=[INF]*N
    d[0]=0
    que=deque()
    que.append(0)
    while que:
        v=que.popleft()
        for nv in edge[v]:
            if d[nv]>d[v]+1:
                d[nv]=d[v]+1
                que.append(nv)
    for i in range(N):
        print(i+1,d[i] if d[i]!=INF else -1)

if __name__ == '__main__':
    main()

