import sys
# sys.stdin = open('input.txt')
from collections import deque

def fillMatrix():
    for i in range(n):
        u, k, *v = map(int, input().split())
        for j in v:
            matrix[i][j-1] = 1

def bfs(s):
    status[s] = VISITING
    distance[s] = 0
    Q.append(s)
    while len(Q) > 0:
        u = Q.popleft()
        for v in range(n):
            if matrix[u][v] and status[v] == NOT_VISITED:
                status[v] = VISITING
                distance[v] = distance[u] + 1
                Q.append(v)
        status[u] = VISITED

n = int(input())
matrix = [[0]*n for i in range(n)]
INFTY = 1 << 21
distance = [INFTY] * n

NOT_VISITED = 0 # WHITE
VISITING = 1 # GRAY
VISITED = 2 # BLACK
status = [NOT_VISITED] * n
Q = deque()

fillMatrix()
bfs(0)

for i in range(n):
    ans = []
    ans.append(i+1)
    if distance[i] == INFTY:
        distance[i] = -1
    ans.append(distance[i])
    ans = map(str, ans)
    print(' '.join(ans)) import sys sys from collections import deque def fillMatrix():
    for i in range(n):
        u, k, *v = map(int, input().split())
        for j in v:
            matrix[i][j-1] = 1 fillMatrix for i in range(n):
        u, k, *v = map(int, input().split())
        for j in v:
            matrix[i][j-1] = 1 i range(n) range n u, k, *v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *v v for j in v:
            matrix[i][j-1] = 1 j v matrix[i][j-1] = 1 matrix[i][j-1] [i] matrix i j-1 j 1 1 def bfs(s):
    status[s] = VISITING
    distance[s] = 0
    Q.append(s)
    while len(Q) > 0:
        u = Q.popleft()
        for v in range(n):
            if matrix[u][v] and status[v] == NOT_VISITED:
                status[v] = VISITING
                distance[v] = distance[u] + 1
                Q.append(v)
        status[u] = VISITED bfs status[s] = VISITING status[s] status s VISITING distance[s] = 0 distance[s] distance s 0 Q.append(s) Q.append Q append s while len(Q) > 0:
        u = Q.popleft()
        for v in range(n):
            if matrix[u][v] and status[v] == NOT_VISITED:
                status[v] = VISITING
                distance[v] = distance[u] + 1
                Q.append(v)
        status[u] = VISITED len(Q) > 0 len(Q) len Q 0 u = Q.popleft() u Q.popleft() Q.popleft Q popleft for v in range(n):
            if matrix[u][v] and status[v] == NOT_VISITED:
                status[v] = VISITING
                distance[v] = distance[u] + 1
                Q.append(v)
         v range(n) range n if matrix[u][v] and status[v] == NOT_VISITED:
                status[v] = VISITING
                distance[v] = distance[u] + 1
                Q.append(v)
         matrix[u][v] and status[v] == NOT_VISITED matrix[u][v] [u] matrix u v status[v] == NOT_VISITED status[v] status v NOT_VISITED status[v] = VISITING status[v] status v VISITING distance[v] = distance[u] + 1 distance[v] distance v distance[u] + 1 distance[u] distance u 1 Q.append(v) Q.append Q append v status[u] = VISITED status[u] status u VISITED s s n = int(input()) n int(input()) int input() input [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 matrix = [[0]*n for i in range(n)] matrix [[0]*n for i in range(n)] INFTY = 1 << 21 INFTY 1 << 21 1 21 distance = [INFTY] * n distance [INFTY] * n [INFTY] INFTY n NOT_VISITED = 0 NOT_VISITED 0 VISITING = 1 VISITING 1 VISITED = 2 VISITED 2 status = [NOT_VISITED] * n status [NOT_VISITED] * n [NOT_VISITED] NOT_VISITED n Q = deque() Q deque() deque fillMatrix() fillMatrix bfs(0) bfs 0 for i in range(n):
    ans = []
    ans.append(i+1)
    if distance[i] == INFTY:
        distance[i] = -1
    ans.append(distance[i])
    ans = map(str, ans)
    print(' '.join(ans)) i range(n) range n ans = [] ans [] ans.append(i+1) ans.append ans append i+1 i 1 if distance[i] == INFTY:
        distance[i] = -1
     distance[i] == INFTY distance[i] distance i INFTY distance[i] = -1 distance[i] distance i -1 ans.append(distance[i]) ans.append ans append distance[i] distance i ans = map(str, ans) ans map(str, ans) map str ans print(' '.join(ans)) print ' '.join(ans) ' '.join ' ' join ans n = int(input()) int(input()) n INFTY = 1 << 21 1 << 21 INFTY ans = [] [] ans ans = map(str, ans) map(str, ans) ans Q = deque() deque() Q VISITING = 1 1 VISITING matrix = [[0]*n for i in range(n)] [[0]*n for i in range(n)] matrix def bfs(s):
    status[s] = VISITING
    distance[s] = 0
    Q.append(s)
    while len(Q) > 0:
        u = Q.popleft()
        for v in range(n):
            if matrix[u][v] and status[v] == NOT_VISITED:
                status[v] = VISITING
                distance[v] = distance[u] + 1
                Q.append(v)
        status[u] = VISITED def bfs(s):
    status[s] = VISITING
    distance[s] = 0
    Q.append(s)
    while len(Q) > 0:
        u = Q.popleft()
        for v in range(n):
            if matrix[u][v] and status[v] == NOT_VISITED:
                status[v] = VISITING
                distance[v] = distance[u] + 1
                Q.append(v)
        status[u] = VISITED bfs def fillMatrix():
    for i in range(n):
        u, k, *v = map(int, input().split())
        for j in v:
            matrix[i][j-1] = 1 def fillMatrix():
    for i in range(n):
        u, k, *v = map(int, input().split())
        for j in v:
            matrix[i][j-1] = 1 fillMatrix VISITED = 2 2 VISITED distance = [INFTY] * n [INFTY] * n distance status = [NOT_VISITED] * n [NOT_VISITED] * n status NOT_VISITED = 0 0 NOT_VISITED
import sys
# sys.stdin = open('input.txt')
from collections import deque

def fillMatrix():
    for i in range(n):
        u, k, *v = map(int, input().split())
        for j in v:
            matrix[i][j-1] = 1

def bfs(s):
    status[s] = VISITING
    distance[s] = 0
    Q.append(s)
    while len(Q) > 0:
        u = Q.popleft()
        for v in range(n):
            if matrix[u][v] and status[v] == NOT_VISITED:
                status[v] = VISITING
                distance[v] = distance[u] + 1
                Q.append(v)
        status[u] = VISITED

n = int(input())
matrix = [[0]*n for i in range(n)]
INFTY = 1 << 21
distance = [INFTY] * n

NOT_VISITED = 0 # WHITE
VISITING = 1 # GRAY
VISITED = 2 # BLACK
status = [NOT_VISITED] * n
Q = deque()

fillMatrix()
bfs(0)

for i in range(n):
    ans = []
    ans.append(i+1)
    if distance[i] == INFTY:
        distance[i] = -1
    ans.append(distance[i])
    ans = map(str, ans)
    print(' '.join(ans))
