
def bfs(s):  # たぶんstartのs
    global n, M, color, d, q

    q.append(s)
    d[s] = 0

    while len(q) != 0:
        u = q.pop(0)

        for v in range(n):
            if M[u][v] != 1:
                continue

            if color[v] == 'WHITE':
                q.append(v)
                color[v] = 'GRAY'
                d[v] = d[u] + 1

        color[u] = 'BLACK'


n = int(input())

M = [[0 for i in range(n)] for j in range(n)]
color = ['WHITE'] * n  # 各頂点の色を記録。ホワイト（未探索）、グレイ（探索中）、ブラック（探索ずみ）
d = [-1] * n  # 始点0からvまでの距離
q = []

for i in range(n):
    _ = list(map(int, input().split()))
    u = _[0] - 1  # 0オリジンに変換

    degree = _[1]
    for j in range(degree):
        v = _[j+2] - 1  # 0オリジンに変換
        M[u][v] = 1

# for row in M:
#     print(' '.join(map(str, row)))

bfs(0)

for i in range(n):
    print('{} {}'.format(i+1, d[i]))
 def bfs(s):  # たぶんstartのs
    global n, M, color, d, q

    q.append(s)
    d[s] = 0

    while len(q) != 0:
        u = q.pop(0)

        for v in range(n):
            if M[u][v] != 1:
                continue

            if color[v] == 'WHITE':
                q.append(v)
                color[v] = 'GRAY'
                d[v] = d[u] + 1

        color[u] = 'BLACK' bfs global n, M, color, d, q q.append(s) q.append q append s d[s] = 0 d[s] d s 0 while len(q) != 0:
        u = q.pop(0)

        for v in range(n):
            if M[u][v] != 1:
                continue

            if color[v] == 'WHITE':
                q.append(v)
                color[v] = 'GRAY'
                d[v] = d[u] + 1

        color[u] = 'BLACK' len(q) != 0 len(q) len q 0 u = q.pop(0) u q.pop(0) q.pop q pop 0 for v in range(n):
            if M[u][v] != 1:
                continue

            if color[v] == 'WHITE':
                q.append(v)
                color[v] = 'GRAY'
                d[v] = d[u] + 1

         v range(n) range n if M[u][v] != 1:
                continue

             M[u][v] != 1 M[u][v] [u] M u v 1 continue if color[v] == 'WHITE':
                q.append(v)
                color[v] = 'GRAY'
                d[v] = d[u] + 1

         color[v] == 'WHITE' color[v] color v 'WHITE' q.append(v) q.append q append v 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 color[u] = 'BLACK' color[u] color u 'BLACK' s s n = int(input()) n int(input()) int input() input [0 for i in range(n)] for j in range(n) for j in range(n) j range(n) range n for j in range(n) [0 for i in range(n)] M = [[0 for i in range(n)] for j in range(n)] M [[0 for i in range(n)] for j in range(n)] color = ['WHITE'] * n color ['WHITE'] * n ['WHITE'] 'WHITE' n d = [-1] * n d [-1] * n [-1] -1 n q = [] q [] for i in range(n):
    _ = list(map(int, input().split()))
    u = _[0] - 1  # 0オリジンに変換

    degree = _[1]
    for j in range(degree):
        v = _[j+2] - 1  # 0オリジンに変換
        M[u][v] = 1

# for row in M:
#     print(' '.join(map(str, row))) i range(n) range n _ = list(map(int, input().split())) _ list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = _[0] - 1 u _[0] - 1 _[0] _ 0 1 degree = _[1] degree _[1] _ 1 for j in range(degree):
        v = _[j+2] - 1  # 0オリジンに変換
        M[u][v] = 1

# for row in M:
#     print(' '.join(map(str, row))) j range(degree) range degree v = _[j+2] - 1 v _[j+2] - 1 _[j+2] _ j+2 j 2 1 M[u][v] = 1 M[u][v] [u] M u v 1 bfs(0) bfs 0 for i in range(n):
    print('{} {}'.format(i+1, d[i])) i range(n) range n print('{} {}'.format(i+1, d[i])) print '{} {}'.format(i+1, d[i]) '{} {}'.format '{} {}' format i+1 i 1 d[i] d i color = ['WHITE'] * n ['WHITE'] * n color degree = _[1] _[1] degree def bfs(s):  # たぶんstartのs
    global n, M, color, d, q

    q.append(s)
    d[s] = 0

    while len(q) != 0:
        u = q.pop(0)

        for v in range(n):
            if M[u][v] != 1:
                continue

            if color[v] == 'WHITE':
                q.append(v)
                color[v] = 'GRAY'
                d[v] = d[u] + 1

        color[u] = 'BLACK' def bfs(s):  # たぶんstartのs
    global n, M, color, d, q

    q.append(s)
    d[s] = 0

    while len(q) != 0:
        u = q.pop(0)

        for v in range(n):
            if M[u][v] != 1:
                continue

            if color[v] == 'WHITE':
                q.append(v)
                color[v] = 'GRAY'
                d[v] = d[u] + 1

        color[u] = 'BLACK' bfs n = int(input()) int(input()) n _ = list(map(int, input().split())) list(map(int, input().split())) _ u = _[0] - 1 _[0] - 1 u d = [-1] * n [-1] * n d q = [] [] q v = _[j+2] - 1 _[j+2] - 1 v M = [[0 for i in range(n)] for j in range(n)] [[0 for i in range(n)] for j in range(n)] M

def bfs(s):  # たぶんstartのs
    global n, M, color, d, q

    q.append(s)
    d[s] = 0

    while len(q) != 0:
        u = q.pop(0)

        for v in range(n):
            if M[u][v] != 1:
                continue

            if color[v] == 'WHITE':
                q.append(v)
                color[v] = 'GRAY'
                d[v] = d[u] + 1

        color[u] = 'BLACK'


n = int(input())

M = [[0 for i in range(n)] for j in range(n)]
color = ['WHITE'] * n  # 各頂点の色を記録。ホワイト（未探索）、グレイ（探索中）、ブラック（探索ずみ）
d = [-1] * n  # 始点0からvまでの距離
q = []

for i in range(n):
    _ = list(map(int, input().split()))
    u = _[0] - 1  # 0オリジンに変換

    degree = _[1]
    for j in range(degree):
        v = _[j+2] - 1  # 0オリジンに変換
        M[u][v] = 1

# for row in M:
#     print(' '.join(map(str, row)))

bfs(0)

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

