from collections import deque
import sys

INFTY=sys.maxsize
WHITE=0
GRAY=1
BLACK=2

color=[]
d=[]
Q=deque([])
n=0
L=[]

def readinput():
    global n
    global L

    n=int(input())
    for i in range(n+1):
        L.append([])

    for i in range(n):
        inp=list(map(int,input().split()))
        L[inp[0]]=inp[2:]


def bfs(s):
    global color
    global d
    global Q

    for i in range(n+1):
        color=[WHITE]*(n+1)
        d=[-1]*(n+1)

    color[s]=GRAY
    d[s]=0
    Q.append(s)

    while(len(Q)>0):
        u = Q.popleft()
        for l in L[u]:
            if(color[l]==WHITE):
                color[l]=GRAY
                d[l]=d[u]+1
                Q.append(l)
        color[u]=BLACK

if __name__=='__main__':
    readinput()
    bfs(1)
    for i in range(1,n+1):
        print('{} {}'.format(i, d[i]))
 from collections import deque import sys sys INFTY=sys.maxsize INFTY sys.maxsize sys maxsize WHITE=0 WHITE 0 GRAY=1 GRAY 1 BLACK=2 BLACK 2 color=[] color [] d=[] d [] Q=deque([]) Q deque([]) deque [] n=0 n 0 L=[] L [] def readinput():
    global n
    global L

    n=int(input())
    for i in range(n+1):
        L.append([])

    for i in range(n):
        inp=list(map(int,input().split()))
        L[inp[0]]=inp[2:] readinput global n global L n=int(input()) n int(input()) int input() input for i in range(n+1):
        L.append([])

     i range(n+1) range n+1 n 1 L.append([]) L.append L append [] for i in range(n):
        inp=list(map(int,input().split()))
        L[inp[0]]=inp[2:] i range(n) range n inp=list(map(int,input().split())) inp list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split L[inp[0]]=inp[2:] L[inp[0]] L inp[0] inp 0 inp[2:] inp 2: 2 def bfs(s):
    global color
    global d
    global Q

    for i in range(n+1):
        color=[WHITE]*(n+1)
        d=[-1]*(n+1)

    color[s]=GRAY
    d[s]=0
    Q.append(s)

    while(len(Q)>0):
        u = Q.popleft()
        for l in L[u]:
            if(color[l]==WHITE):
                color[l]=GRAY
                d[l]=d[u]+1
                Q.append(l)
        color[u]=BLACK bfs global color global d global Q for i in range(n+1):
        color=[WHITE]*(n+1)
        d=[-1]*(n+1)

     i range(n+1) range n+1 n 1 color=[WHITE]*(n+1) color [WHITE]*(n+1) [WHITE] WHITE n+1 n 1 d=[-1]*(n+1) d [-1]*(n+1) [-1] -1 n+1 n 1 color[s]=GRAY color[s] color s GRAY d[s]=0 d[s] d s 0 Q.append(s) Q.append Q append s while(len(Q)>0):
        u = Q.popleft()
        for l in L[u]:
            if(color[l]==WHITE):
                color[l]=GRAY
                d[l]=d[u]+1
                Q.append(l)
        color[u]=BLACK len(Q)>0 len(Q) len Q 0 u = Q.popleft() u Q.popleft() Q.popleft Q popleft for l in L[u]:
            if(color[l]==WHITE):
                color[l]=GRAY
                d[l]=d[u]+1
                Q.append(l)
         l L[u] L u if(color[l]==WHITE):
                color[l]=GRAY
                d[l]=d[u]+1
                Q.append(l)
         color[l]==WHITE color[l] color l WHITE color[l]=GRAY color[l] color l GRAY d[l]=d[u]+1 d[l] d l d[u]+1 d[u] d u 1 Q.append(l) Q.append Q append l color[u]=BLACK color[u] color u BLACK s s if __name__=='__main__':
    readinput()
    bfs(1)
    for i in range(1,n+1):
        print('{} {}'.format(i, d[i])) __name__=='__main__' __name__ '__main__' readinput() readinput bfs(1) bfs 1 for i in range(1,n+1):
        print('{} {}'.format(i, d[i])) i range(1,n+1) range 1 n+1 n 1 print('{} {}'.format(i, d[i])) print '{} {}'.format(i, d[i]) '{} {}'.format '{} {}' format i d[i] d i def bfs(s):
    global color
    global d
    global Q

    for i in range(n+1):
        color=[WHITE]*(n+1)
        d=[-1]*(n+1)

    color[s]=GRAY
    d[s]=0
    Q.append(s)

    while(len(Q)>0):
        u = Q.popleft()
        for l in L[u]:
            if(color[l]==WHITE):
                color[l]=GRAY
                d[l]=d[u]+1
                Q.append(l)
        color[u]=BLACK def bfs(s):
    global color
    global d
    global Q

    for i in range(n+1):
        color=[WHITE]*(n+1)
        d=[-1]*(n+1)

    color[s]=GRAY
    d[s]=0
    Q.append(s)

    while(len(Q)>0):
        u = Q.popleft()
        for l in L[u]:
            if(color[l]==WHITE):
                color[l]=GRAY
                d[l]=d[u]+1
                Q.append(l)
        color[u]=BLACK bfs color=[] [] color n=0 0 n d=[] [] d L=[] [] L def readinput():
    global n
    global L

    n=int(input())
    for i in range(n+1):
        L.append([])

    for i in range(n):
        inp=list(map(int,input().split()))
        L[inp[0]]=inp[2:] def readinput():
    global n
    global L

    n=int(input())
    for i in range(n+1):
        L.append([])

    for i in range(n):
        inp=list(map(int,input().split()))
        L[inp[0]]=inp[2:] readinput BLACK=2 2 BLACK WHITE=0 0 WHITE Q=deque([]) deque([]) Q INFTY=sys.maxsize sys.maxsize INFTY GRAY=1 1 GRAY
from collections import deque
import sys

INFTY=sys.maxsize
WHITE=0
GRAY=1
BLACK=2

color=[]
d=[]
Q=deque([])
n=0
L=[]

def readinput():
    global n
    global L

    n=int(input())
    for i in range(n+1):
        L.append([])

    for i in range(n):
        inp=list(map(int,input().split()))
        L[inp[0]]=inp[2:]


def bfs(s):
    global color
    global d
    global Q

    for i in range(n+1):
        color=[WHITE]*(n+1)
        d=[-1]*(n+1)

    color[s]=GRAY
    d[s]=0
    Q.append(s)

    while(len(Q)>0):
        u = Q.popleft()
        for l in L[u]:
            if(color[l]==WHITE):
                color[l]=GRAY
                d[l]=d[u]+1
                Q.append(l)
        color[u]=BLACK

if __name__=='__main__':
    readinput()
    bfs(1)
    for i in range(1,n+1):
        print('{} {}'.format(i, d[i]))

