from collections import deque

n = int(input())
m = [[0 for j in range(n)] for i in range(n)]
color = [0 for i in range(n)]
q = deque()
dist = [-1 for i in range(n)]


def bfs(s):
    color[s], dist[s] = 1, 0
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(n):
            if m[u][v] == 1 and color[v] == 0:
                color[v], dist[v] = 1, dist[u] + 1
                q.append(v)

        color[u] = 2


def main():
    for i in range(n):
        data = list(map(int, input().split()))
        for j in range(2, len(data)):
            m[data[0]-1][data[j]-1] = 1

    bfs(0)

    for i in range(n):
        print(i+1, dist[i])


if __name__ == '__main__':
    main()
 from collections import deque n = int(input()) n int(input()) int input() input [0 for j in range(n)] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [0 for j in range(n)] m = [[0 for j in range(n)] for i in range(n)] m [[0 for j in range(n)] for i in range(n)] 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 color = [0 for i in range(n)] color [0 for i in range(n)] q = deque() q deque() deque -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 dist = [-1 for i in range(n)] dist [-1 for i in range(n)] def bfs(s):
    color[s], dist[s] = 1, 0
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(n):
            if m[u][v] == 1 and color[v] == 0:
                color[v], dist[v] = 1, dist[u] + 1
                q.append(v)

        color[u] = 2 bfs color[s], dist[s] = 1, 0 color[s] color s 1 dist[s] dist s 0 q.append(s) q.append q append s while len(q) != 0:
        u = q.popleft()
        for v in range(n):
            if m[u][v] == 1 and color[v] == 0:
                color[v], dist[v] = 1, dist[u] + 1
                q.append(v)

        color[u] = 2 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] == 1 and color[v] == 0:
                color[v], dist[v] = 1, dist[u] + 1
                q.append(v)

         v range(n) range n if m[u][v] == 1 and color[v] == 0:
                color[v], dist[v] = 1, dist[u] + 1
                q.append(v)

         m[u][v] == 1 and color[v] == 0 m[u][v] == 1 m[u][v] [u] m u v 1 color[v] == 0 color[v] color v 0 color[v], dist[v] = 1, dist[u] + 1 color[v] color v 1 dist[v] dist v dist[u] + 1 dist[u] dist u 1 q.append(v) q.append q append v color[u] = 2 color[u] color u 2 s s def main():
    for i in range(n):
        data = list(map(int, input().split()))
        for j in range(2, len(data)):
            m[data[0]-1][data[j]-1] = 1

    bfs(0)

    for i in range(n):
        print(i+1, dist[i]) main for i in range(n):
        data = list(map(int, input().split()))
        for j in range(2, len(data)):
            m[data[0]-1][data[j]-1] = 1

     i range(n) range n data = list(map(int, input().split())) data list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split for j in range(2, len(data)):
            m[data[0]-1][data[j]-1] = 1

     j range(2, len(data)) range 2 len(data) len data m[data[0]-1][data[j]-1] = 1 m[data[0]-1][data[j]-1] [data[0]-1] m data[0]-1 data[0] data 0 1 data[j]-1 data[j] data j 1 1 bfs(0) bfs 0 for i in range(n):
        print(i+1, dist[i]) i range(n) range n print(i+1, dist[i]) print i+1 i 1 dist[i] dist i if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main m = [[0 for j in range(n)] for i in range(n)] [[0 for j in range(n)] for i in range(n)] m dist = [-1 for i in range(n)] [-1 for i in range(n)] dist def bfs(s):
    color[s], dist[s] = 1, 0
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(n):
            if m[u][v] == 1 and color[v] == 0:
                color[v], dist[v] = 1, dist[u] + 1
                q.append(v)

        color[u] = 2 def bfs(s):
    color[s], dist[s] = 1, 0
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(n):
            if m[u][v] == 1 and color[v] == 0:
                color[v], dist[v] = 1, dist[u] + 1
                q.append(v)

        color[u] = 2 bfs q = deque() deque() q n = int(input()) int(input()) n def main():
    for i in range(n):
        data = list(map(int, input().split()))
        for j in range(2, len(data)):
            m[data[0]-1][data[j]-1] = 1

    bfs(0)

    for i in range(n):
        print(i+1, dist[i]) def main():
    for i in range(n):
        data = list(map(int, input().split()))
        for j in range(2, len(data)):
            m[data[0]-1][data[j]-1] = 1

    bfs(0)

    for i in range(n):
        print(i+1, dist[i]) main color = [0 for i in range(n)] [0 for i in range(n)] color
from collections import deque

n = int(input())
m = [[0 for j in range(n)] for i in range(n)]
color = [0 for i in range(n)]
q = deque()
dist = [-1 for i in range(n)]


def bfs(s):
    color[s], dist[s] = 1, 0
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(n):
            if m[u][v] == 1 and color[v] == 0:
                color[v], dist[v] = 1, dist[u] + 1
                q.append(v)

        color[u] = 2


def main():
    for i in range(n):
        data = list(map(int, input().split()))
        for j in range(2, len(data)):
            m[data[0]-1][data[j]-1] = 1

    bfs(0)

    for i in range(n):
        print(i+1, dist[i])


if __name__ == '__main__':
    main()

