from collections import deque
N = input()
d = [float('inf')] * N
m = [[0] * N for i in range(N)]
Q = deque([])

def bfs(s):
    Q.append(s)
    d[s] = 0

    while(len(Q) != 0):
        u = Q.popleft()
        for v in range(N):
            if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v)




if __name__ == '__main__':

    for i in range(N):
        v = map(int, raw_input().split())
        for j in v[2:]:
            m[i][j-1] = 1
    
    bfs(0)

    for i in range(N):
        if d[i] == float('inf'):
            d[i] = -1
        print (i+1), d[i] from collections import deque N = input() N input() input d = [float('inf')] * N d [float('inf')] * N [float('inf')] float('inf') float 'inf' N [0] * N for i in range(N) for i in range(N) i range(N) range N for i in range(N) [0] * N [0] 0 N m = [[0] * N for i in range(N)] m [[0] * N for i in range(N)] Q = deque([]) Q deque([]) deque [] def bfs(s):
    Q.append(s)
    d[s] = 0

    while(len(Q) != 0):
        u = Q.popleft()
        for v in range(N):
            if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v) bfs Q.append(s) Q.append Q append s d[s] = 0 d[s] d s 0 while(len(Q) != 0):
        u = Q.popleft()
        for v in range(N):
            if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v) len(Q) != 0 len(Q) len Q 0 u = Q.popleft() u Q.popleft() Q.popleft Q popleft for v in range(N):
            if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v) v range(N) range N if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v) m[u][v] and  d[v] == float('inf') m[u][v] [u] m u v d[v] == float('inf') d[v] d v float('inf') float 'inf' 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 s s if __name__ == '__main__':

    for i in range(N):
        v = map(int, raw_input().split())
        for j in v[2:]:
            m[i][j-1] = 1
    
    bfs(0)

    for i in range(N):
        if d[i] == float('inf'):
            d[i] = -1
        print (i+1), d[i] __name__ == '__main__' __name__ '__main__' for i in range(N):
        v = map(int, raw_input().split())
        for j in v[2:]:
            m[i][j-1] = 1
    
     i range(N) range N v = map(int, raw_input().split()) v map(int, raw_input().split()) map int raw_input().split() ().split () raw_input split for j in v[2:]:
            m[i][j-1] = 1
    
     j v[2:] v 2: 2 m[i][j-1] = 1 m[i][j-1] [i] m i j-1 j 1 1 bfs(0) bfs 0 for i in range(N):
        if d[i] == float('inf'):
            d[i] = -1
        print (i+1), d[i] i range(N) range N if d[i] == float('inf'):
            d[i] = -1
         d[i] == float('inf') d[i] d i float('inf') float 'inf' d[i] = -1 d[i] d i -1 print (i+1) print i+1 i 1 d[i] d i d = [float('inf')] * N [float('inf')] * N d v = map(int, raw_input().split()) map(int, raw_input().split()) v def bfs(s):
    Q.append(s)
    d[s] = 0

    while(len(Q) != 0):
        u = Q.popleft()
        for v in range(N):
            if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v) def bfs(s):
    Q.append(s)
    d[s] = 0

    while(len(Q) != 0):
        u = Q.popleft()
        for v in range(N):
            if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v) bfs N = input() input() N m = [[0] * N for i in range(N)] [[0] * N for i in range(N)] m Q = deque([]) deque([]) Q
from collections import deque
N = input()
d = [float('inf')] * N
m = [[0] * N for i in range(N)]
Q = deque([])

def bfs(s):
    Q.append(s)
    d[s] = 0

    while(len(Q) != 0):
        u = Q.popleft()
        for v in range(N):
            if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v)




if __name__ == '__main__':

    for i in range(N):
        v = map(int, raw_input().split())
        for j in v[2:]:
            m[i][j-1] = 1
    
    bfs(0)

    for i in range(N):
        if d[i] == float('inf'):
            d[i] = -1
        print (i+1), d[i]