n=int(input())
e=[[] for _ in range(n)]
for _ in range(n):
    u,_,*v=map(lambda x:int(x)-1, input().split())
    e[u]=sorted(v)
q=[0]
d=[-1]*n
d[0]=0
while q:
    i=q.pop(0)
    for v in e[i]:
        if d[v]==-1:
            q.append(v)
            d[v]=d[i]+1
for i in range(n):
    print(i+1,d[i])
 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)] for _ in range(n):
    u,_,*v=map(lambda x:int(x)-1, input().split())
    e[u]=sorted(v) _ range(n) range n u,_,*v=map(lambda x:int(x)-1, input().split()) u 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 _ *v v e[u]=sorted(v) e[u] e u sorted(v) sorted v q=[0] q [0] 0 d=[-1]*n d [-1]*n [-1] -1 n d[0]=0 d[0] d 0 0 while q:
    i=q.pop(0)
    for v in e[i]:
        if d[v]==-1:
            q.append(v)
            d[v]=d[i]+1 q i=q.pop(0) i q.pop(0) q.pop q pop 0 for v in e[i]:
        if d[v]==-1:
            q.append(v)
            d[v]=d[i]+1 v e[i] e i if d[v]==-1:
            q.append(v)
            d[v]=d[i]+1 d[v]==-1 d[v] d v -1 q.append(v) q.append q append v d[v]=d[i]+1 d[v] d v d[i]+1 d[i] d i 1 for i in range(n):
    print(i+1,d[i]) i range(n) range n print(i+1,d[i]) print i+1 i 1 d[i] d i e=[[] for _ in range(n)] [[] for _ in range(n)] e u,_,*v=map(lambda x:int(x)-1, input().split()) map(lambda x:int(x)-1, input().split()) u _,*v=map(lambda x:int(x)-1, input().split()) map(lambda x:int(x)-1, input().split()) _ n=int(input()) int(input()) n i=q.pop(0) q.pop(0) i q=[0] [0] q d=[-1]*n [-1]*n d
n=int(input())
e=[[] for _ in range(n)]
for _ in range(n):
    u,_,*v=map(lambda x:int(x)-1, input().split())
    e[u]=sorted(v)
q=[0]
d=[-1]*n
d[0]=0
while q:
    i=q.pop(0)
    for v in e[i]:
        if d[v]==-1:
            q.append(v)
            d[v]=d[i]+1
for i in range(n):
    print(i+1,d[i])

