from collections import deque

def bfs(adj_mat, d):
    Q = deque()
    N = len(d)
    color = ["W" for n in range(N)]

    Q.append(0)
    d[0] = 0

    while len(Q) != 0:
        u = Q.popleft()

        for i in range(N):
            if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

        color[u] = "B"

    for i in range(N):
        if color[i] != "B":
            d[i] = -1

def Main():
    N = int(input())

    adj_mat = [[0 for n in range(N)] for n in range(N)]
    d = [0] * N

    for n in range(N):
        u, k , *V = map(int, input().split())

        if k > 0:
            for i in V:
                adj_mat[n][i - 1] = 1

    bfs(adj_mat, d)

    for n in range(N):
        print("{} {}".format(n + 1, d[n]))
Main() from collections import deque def bfs(adj_mat, d):
    Q = deque()
    N = len(d)
    color = ["W" for n in range(N)]

    Q.append(0)
    d[0] = 0

    while len(Q) != 0:
        u = Q.popleft()

        for i in range(N):
            if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

        color[u] = "B"

    for i in range(N):
        if color[i] != "B":
            d[i] = -1 bfs Q = deque() Q deque() deque N = len(d) N len(d) len d "W" for n in range(N) for n in range(N) n range(N) range N for n in range(N) "W" color = ["W" for n in range(N)] color ["W" for n in range(N)] Q.append(0) Q.append Q append 0 d[0] = 0 d[0] d 0 0 while len(Q) != 0:
        u = Q.popleft()

        for i in range(N):
            if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

        color[u] = "B"

     len(Q) != 0 len(Q) len Q 0 u = Q.popleft() u Q.popleft() Q.popleft Q popleft for i in range(N):
            if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

         i range(N) range N if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

         adj_mat[u][i] == 1 and color[i] == "W" adj_mat[u][i] == 1 adj_mat[u][i] [u] adj_mat u i 1 color[i] == "W" color[i] color i "W" color[i] = "G" color[i] color i "G" d[i] = d[u] + 1 d[i] d i d[u] + 1 d[u] d u 1 Q.append(i) Q.append Q append i color[u] = "B" color[u] color u "B" for i in range(N):
        if color[i] != "B":
            d[i] = -1 i range(N) range N if color[i] != "B":
            d[i] = -1 color[i] != "B" color[i] color i "B" d[i] = -1 d[i] d i -1 adj_mat adj_mat d d def Main():
    N = int(input())

    adj_mat = [[0 for n in range(N)] for n in range(N)]
    d = [0] * N

    for n in range(N):
        u, k , *V = map(int, input().split())

        if k > 0:
            for i in V:
                adj_mat[n][i - 1] = 1

    bfs(adj_mat, d)

    for n in range(N):
        print("{} {}".format(n + 1, d[n])) Main N = int(input()) N int(input()) int input() input [0 for n in range(N)] for n in range(N) for n in range(N) n range(N) range N for n in range(N) [0 for n in range(N)] adj_mat = [[0 for n in range(N)] for n in range(N)] adj_mat [[0 for n in range(N)] for n in range(N)] d = [0] * N d [0] * N [0] 0 N for n in range(N):
        u, k , *V = map(int, input().split())

        if k > 0:
            for i in V:
                adj_mat[n][i - 1] = 1

     n 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 if k > 0:
            for i in V:
                adj_mat[n][i - 1] = 1

     k > 0 k 0 for i in V:
                adj_mat[n][i - 1] = 1

     i V adj_mat[n][i - 1] = 1 adj_mat[n][i - 1] [n] adj_mat n i - 1 i 1 1 bfs(adj_mat, d) bfs adj_mat d for n in range(N):
        print("{} {}".format(n + 1, d[n])) n range(N) range N print("{} {}".format(n + 1, d[n])) print "{} {}".format(n + 1, d[n]) "{} {}".format "{} {}" format n + 1 n 1 d[n] d n Main() Main def bfs(adj_mat, d):
    Q = deque()
    N = len(d)
    color = ["W" for n in range(N)]

    Q.append(0)
    d[0] = 0

    while len(Q) != 0:
        u = Q.popleft()

        for i in range(N):
            if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

        color[u] = "B"

    for i in range(N):
        if color[i] != "B":
            d[i] = -1 def bfs(adj_mat, d):
    Q = deque()
    N = len(d)
    color = ["W" for n in range(N)]

    Q.append(0)
    d[0] = 0

    while len(Q) != 0:
        u = Q.popleft()

        for i in range(N):
            if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

        color[u] = "B"

    for i in range(N):
        if color[i] != "B":
            d[i] = -1 bfs def Main():
    N = int(input())

    adj_mat = [[0 for n in range(N)] for n in range(N)]
    d = [0] * N

    for n in range(N):
        u, k , *V = map(int, input().split())

        if k > 0:
            for i in V:
                adj_mat[n][i - 1] = 1

    bfs(adj_mat, d)

    for n in range(N):
        print("{} {}".format(n + 1, d[n])) def Main():
    N = int(input())

    adj_mat = [[0 for n in range(N)] for n in range(N)]
    d = [0] * N

    for n in range(N):
        u, k , *V = map(int, input().split())

        if k > 0:
            for i in V:
                adj_mat[n][i - 1] = 1

    bfs(adj_mat, d)

    for n in range(N):
        print("{} {}".format(n + 1, d[n])) Main
from collections import deque

def bfs(adj_mat, d):
    Q = deque()
    N = len(d)
    color = ["W" for n in range(N)]

    Q.append(0)
    d[0] = 0

    while len(Q) != 0:
        u = Q.popleft()

        for i in range(N):
            if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

        color[u] = "B"

    for i in range(N):
        if color[i] != "B":
            d[i] = -1

def Main():
    N = int(input())

    adj_mat = [[0 for n in range(N)] for n in range(N)]
    d = [0] * N

    for n in range(N):
        u, k , *V = map(int, input().split())

        if k > 0:
            for i in V:
                adj_mat[n][i - 1] = 1

    bfs(adj_mat, d)

    for n in range(N):
        print("{} {}".format(n + 1, d[n]))
Main()
