from collections import deque
n=int(input())
v=[[] for _ in range(n)]
dist=[n+1]*n
dist[0]=0
for i in range(n):
    l=list(map(int,input().split()))
    if l[1]==0:
        continue
    else:
        for j in l[2:]:
            v[l[0]-1].append(j-1)

flg=1
q=deque()
while flg==1:
    flg=0
    q=deque()
    for i in range(n):
        q.append(i)
        chk=[0]*n
        while q:
            x=q.popleft()
            chk[x]=1
            d=dist[x]
            for j in v[x]:
                if chk[j]==1:
                    continue
                else:
                    if dist[j]>d+1:
                        flg=1
                        dist[j]=d+1
                        q.append(j)

for i in range(n):
    if dist[i]==n+1:
        print(i+1,-1)
    else:
        print(i+1,dist[i]) from collections import deque 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) [] v=[[] for _ in range(n)] v [[] for _ in range(n)] dist=[n+1]*n dist [n+1]*n [n+1] n+1 n 1 n dist[0]=0 dist[0] dist 0 0 for i in range(n):
    l=list(map(int,input().split()))
    if l[1]==0:
        continue
    else:
        for j in l[2:]:
            v[l[0]-1].append(j-1) i range(n) range n l=list(map(int,input().split())) l list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split if l[1]==0:
        continue
    else:
        for j in l[2:]:
            v[l[0]-1].append(j-1) l[1]==0 l[1] l 1 0 continue for j in l[2:]:
            v[l[0]-1].append(j-1) j l[2:] l 2: 2 v[l[0]-1].append(j-1) [l[0]-1].append [l[0]-1] v l[0]-1 l[0] l 0 1 append j-1 j 1 flg=1 flg 1 q=deque() q deque() deque while flg==1:
    flg=0
    q=deque()
    for i in range(n):
        q.append(i)
        chk=[0]*n
        while q:
            x=q.popleft()
            chk[x]=1
            d=dist[x]
            for j in v[x]:
                if chk[j]==1:
                    continue
                else:
                    if dist[j]>d+1:
                        flg=1
                        dist[j]=d+1
                        q.append(j) flg==1 flg 1 flg=0 flg 0 q=deque() q deque() deque for i in range(n):
        q.append(i)
        chk=[0]*n
        while q:
            x=q.popleft()
            chk[x]=1
            d=dist[x]
            for j in v[x]:
                if chk[j]==1:
                    continue
                else:
                    if dist[j]>d+1:
                        flg=1
                        dist[j]=d+1
                        q.append(j) i range(n) range n q.append(i) q.append q append i chk=[0]*n chk [0]*n [0] 0 n while q:
            x=q.popleft()
            chk[x]=1
            d=dist[x]
            for j in v[x]:
                if chk[j]==1:
                    continue
                else:
                    if dist[j]>d+1:
                        flg=1
                        dist[j]=d+1
                        q.append(j) q x=q.popleft() x q.popleft() q.popleft q popleft chk[x]=1 chk[x] chk x 1 d=dist[x] d dist[x] dist x for j in v[x]:
                if chk[j]==1:
                    continue
                else:
                    if dist[j]>d+1:
                        flg=1
                        dist[j]=d+1
                        q.append(j) j v[x] v x if chk[j]==1:
                    continue
                else:
                    if dist[j]>d+1:
                        flg=1
                        dist[j]=d+1
                        q.append(j) chk[j]==1 chk[j] chk j 1 continue if dist[j]>d+1:
                        flg=1
                        dist[j]=d+1
                        q.append(j) dist[j]>d+1 dist[j] dist j d+1 d 1 flg=1 flg 1 dist[j]=d+1 dist[j] dist j d+1 d 1 q.append(j) q.append q append j for i in range(n):
    if dist[i]==n+1:
        print(i+1,-1)
    else:
        print(i+1,dist[i]) i range(n) range n if dist[i]==n+1:
        print(i+1,-1)
    else:
        print(i+1,dist[i]) dist[i]==n+1 dist[i] dist i n+1 n 1 print(i+1,-1) print i+1 i 1 -1 print(i+1,dist[i]) print i+1 i 1 dist[i] dist i chk=[0]*n [0]*n chk flg=1 1 flg dist=[n+1]*n [n+1]*n dist l=list(map(int,input().split())) list(map(int,input().split())) l q=deque() deque() q flg=0 0 flg d=dist[x] dist[x] d n=int(input()) int(input()) n q=deque() deque() q flg=1 1 flg v=[[] for _ in range(n)] [[] for _ in range(n)] v x=q.popleft() q.popleft() x
from collections import deque
n=int(input())
v=[[] for _ in range(n)]
dist=[n+1]*n
dist[0]=0
for i in range(n):
    l=list(map(int,input().split()))
    if l[1]==0:
        continue
    else:
        for j in l[2:]:
            v[l[0]-1].append(j-1)

flg=1
q=deque()
while flg==1:
    flg=0
    q=deque()
    for i in range(n):
        q.append(i)
        chk=[0]*n
        while q:
            x=q.popleft()
            chk[x]=1
            d=dist[x]
            for j in v[x]:
                if chk[j]==1:
                    continue
                else:
                    if dist[j]>d+1:
                        flg=1
                        dist[j]=d+1
                        q.append(j)

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