from collections import deque
def bfs(s):
    color[s] = 'GRAY'
    d[s] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'BLACK'
        
n = int(input())
M = [[0] * n for _ in range(n)]
color = ['WHITE'] * n
d = [-1] * n
Q = deque()

for _ in range(n):
    a = list(map(int, input().split()))
    u = a[0]
    v = a[2:]
    for j in v:
        M[u - 1][j - 1] = 1
        
bfs(0)

for i in range(n):
    out = ''
    out += '{} {}'.format(i + 1, d[i])
    print(out) from collections import deque def bfs(s):
    color[s] = 'GRAY'
    d[s] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'BLACK'
         bfs color[s] = 'GRAY' color[s] color s 'GRAY' d[s] = 0 d[s] d s 0 Q.append(0) Q.append Q append 0 while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'BLACK'
         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 color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
         v range(n) range n if M[u][v] and color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
         M[u][v] and color[v] == 'WHITE' M[u][v] [u] M u v color[v] == 'WHITE' color[v] color v 'WHITE' color[v] = 'GRAY' color[v] color v 'GRAY' 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 color[u] = 'BLACK' color[u] color u 'BLACK' s s n = int(input()) n int(input()) int input() input [0] * n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * n [0] 0 n M = [[0] * n for _ in range(n)] M [[0] * n for _ in range(n)] color = ['WHITE'] * n color ['WHITE'] * n ['WHITE'] 'WHITE' n d = [-1] * n d [-1] * n [-1] -1 n Q = deque() Q deque() deque for _ in range(n):
    a = list(map(int, input().split()))
    u = a[0]
    v = a[2:]
    for j in v:
        M[u - 1][j - 1] = 1
         _ range(n) range n a = list(map(int, input().split())) a list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = a[0] u a[0] a 0 v = a[2:] v a[2:] a 2: 2 for j in v:
        M[u - 1][j - 1] = 1
         j v M[u - 1][j - 1] = 1 M[u - 1][j - 1] [u - 1] M u - 1 u 1 j - 1 j 1 1 bfs(0) bfs 0 for i in range(n):
    out = ''
    out += '{} {}'.format(i + 1, d[i])
    print(out) i range(n) range n out = '' out '' out += '{} {}'.format(i + 1, d[i]) out '{} {}'.format(i + 1, d[i]) '{} {}'.format '{} {}' format i + 1 i 1 d[i] d i print(out) print out n = int(input()) int(input()) n d = [-1] * n [-1] * n d v = a[2:] a[2:] v out += '{} {}'.format(i + 1, d[i]) '{} {}'.format(i + 1, d[i]) out M = [[0] * n for _ in range(n)] [[0] * n for _ in range(n)] M a = list(map(int, input().split())) list(map(int, input().split())) a u = a[0] a[0] u out = '' '' out color = ['WHITE'] * n ['WHITE'] * n color def bfs(s):
    color[s] = 'GRAY'
    d[s] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'BLACK'
         def bfs(s):
    color[s] = 'GRAY'
    d[s] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'BLACK'
         bfs Q = deque() deque() Q
from collections import deque
def bfs(s):
    color[s] = 'GRAY'
    d[s] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'BLACK'
        
n = int(input())
M = [[0] * n for _ in range(n)]
color = ['WHITE'] * n
d = [-1] * n
Q = deque()

for _ in range(n):
    a = list(map(int, input().split()))
    u = a[0]
    v = a[2:]
    for j in v:
        M[u - 1][j - 1] = 1
        
bfs(0)

for i in range(n):
    out = ''
    out += '{} {}'.format(i + 1, d[i])
    print(out)