from collections import deque

n = int(input())

adj = [0]*n # adjacent list
d = [-1]*n # distance from the source vertex
c = ['w']*n # the 'color' of each vertex

# receiving input (the structure of the graph)
for i in range(n):
    tmp = list(map( int, input().split() ) )
    adj[tmp[0]-1] = list(map(lambda x : x - 1, tmp[2:]))

# set the source vertex
s = 0
d[s] = 0
c[s] = 'g'


Q = deque()
Q.append(s)

while Q:
    u = Q.popleft()
    for v in adj[u]:
        if c[v] == 'w':
            c[v] = 'g'
            d[v] = d[u] + 1
            Q.append(v)
    c[u] = 'b'
    

for i in range(n):
    print(f'{i+1} {d[i]}')

 from collections import deque n = int(input()) n int(input()) int input() input adj = [0]*n adj [0]*n [0] 0 n d = [-1]*n d [-1]*n [-1] -1 n c = ['w']*n c ['w']*n ['w'] 'w' n for i in range(n):
    tmp = list(map( int, input().split() ) )
    adj[tmp[0]-1] = list(map(lambda x : x - 1, tmp[2:]))

# set the source vertex i range(n) range n tmp = list(map( int, input().split() ) ) tmp list(map( int, input().split() ) ) list map( int, input().split() ) map int input().split() ().split () input split adj[tmp[0]-1] = list(map(lambda x : x - 1, tmp[2:])) adj[tmp[0]-1] adj tmp[0]-1 tmp[0] tmp 0 1 list(map(lambda x : x - 1, tmp[2:])) list map(lambda x : x - 1, tmp[2:]) map lambda x : x - 1 x - 1 x 1 x tmp[2:] tmp 2: 2 s = 0 s 0 d[s] = 0 d[s] d s 0 c[s] = 'g' c[s] c s 'g' Q = deque() Q deque() deque Q.append(s) Q.append Q append s while Q:
    u = Q.popleft()
    for v in adj[u]:
        if c[v] == 'w':
            c[v] = 'g'
            d[v] = d[u] + 1
            Q.append(v)
    c[u] = 'b'
     Q u = Q.popleft() u Q.popleft() Q.popleft Q popleft for v in adj[u]:
        if c[v] == 'w':
            c[v] = 'g'
            d[v] = d[u] + 1
            Q.append(v)
     v adj[u] adj u if c[v] == 'w':
            c[v] = 'g'
            d[v] = d[u] + 1
            Q.append(v)
     c[v] == 'w' c[v] c v 'w' c[v] = 'g' c[v] c v 'g' d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 Q.append(v) Q.append Q append v c[u] = 'b' c[u] c u 'b' for i in range(n):
    print(f'{i+1} {d[i]}') i range(n) range n print(f'{i+1} {d[i]}') print f'{i+1} {d[i]}' adj = [0]*n [0]*n adj tmp = list(map( int, input().split() ) ) list(map( int, input().split() ) ) tmp Q = deque() deque() Q d = [-1]*n [-1]*n d u = Q.popleft() Q.popleft() u c = ['w']*n ['w']*n c n = int(input()) int(input()) n s = 0 0 s
from collections import deque

n = int(input())

adj = [0]*n # adjacent list
d = [-1]*n # distance from the source vertex
c = ['w']*n # the 'color' of each vertex

# receiving input (the structure of the graph)
for i in range(n):
    tmp = list(map( int, input().split() ) )
    adj[tmp[0]-1] = list(map(lambda x : x - 1, tmp[2:]))

# set the source vertex
s = 0
d[s] = 0
c[s] = 'g'


Q = deque()
Q.append(s)

while Q:
    u = Q.popleft()
    for v in adj[u]:
        if c[v] == 'w':
            c[v] = 'g'
            d[v] = d[u] + 1
            Q.append(v)
    c[u] = 'b'
    

for i in range(n):
    print(f'{i+1} {d[i]}')


