from collections import deque
def bfs(g,v):
    q=deque([v]) ; vis=[0]*n
    vis[v]=1 ; stg[v]=0
    while q:
        v=q[0]
        for p in g[v]:
            if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
        q.popleft()
n=int(input())
g=[[] for i in range(n)]
for _ in range(n):
    a=[int(i)-1 for i in input().split()]
    for i in a[2:]: g[a[0]].append(i)
stg=[-1]*n
bfs(g,0)
for i in range(n):
    print(i+1,stg[i])
    
 from collections import deque def bfs(g,v):
    q=deque([v]) ; vis=[0]*n
    vis[v]=1 ; stg[v]=0
    while q:
        v=q[0]
        for p in g[v]:
            if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
        q.popleft() bfs q=deque([v]) q deque([v]) deque [v] v vis=[0]*n vis [0]*n [0] 0 n vis[v]=1 vis[v] vis v 1 stg[v]=0 stg[v] stg v 0 while q:
        v=q[0]
        for p in g[v]:
            if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
        q.popleft() q v=q[0] v q[0] q 0 for p in g[v]:
            if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
         p g[v] g v if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
         vis[p]==0 vis[p] vis p 0 q.append(p) q.append q append p vis[p]=1 vis[p] vis p 1 stg[p]=stg[v]+1 stg[p] stg p stg[v]+1 stg[v] stg v 1 q.popleft() q.popleft q popleft g g v v n=int(input()) n int(input()) int input() input [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] g=[[] for i in range(n)] g [[] for i in range(n)] for _ in range(n):
    a=[int(i)-1 for i in input().split()]
    for i in a[2:]: g[a[0]].append(i) _ range(n) range n int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 a=[int(i)-1 for i in input().split()] a [int(i)-1 for i in input().split()] for i in a[2:]: g[a[0]].appen i a[2:] a 2: 2 stg=[-1]*n stg [-1]*n [-1] -1 n bfs(g,0) bfs g 0 for i in range(n):
    print(i+1,stg[i])
     i range(n) range n print(i+1,stg[i]) print i+1 i 1 stg[i] stg i a=[int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] a n=int(input()) int(input()) n g=[[] for i in range(n)] [[] for i in range(n)] g stg=[-1]*n [-1]*n stg def bfs(g,v):
    q=deque([v]) ; vis=[0]*n
    vis[v]=1 ; stg[v]=0
    while q:
        v=q[0]
        for p in g[v]:
            if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
        q.popleft() def bfs(g,v):
    q=deque([v]) ; vis=[0]*n
    vis[v]=1 ; stg[v]=0
    while q:
        v=q[0]
        for p in g[v]:
            if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
        q.popleft() bfs
from collections import deque
def bfs(g,v):
    q=deque([v]) ; vis=[0]*n
    vis[v]=1 ; stg[v]=0
    while q:
        v=q[0]
        for p in g[v]:
            if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
        q.popleft()
n=int(input())
g=[[] for i in range(n)]
for _ in range(n):
    a=[int(i)-1 for i in input().split()]
    for i in a[2:]: g[a[0]].append(i)
stg=[-1]*n
bfs(g,0)
for i in range(n):
    print(i+1,stg[i])
    

