# -*- coding: utf-8 -*-
from collections import deque


if __name__ == '__main__':

    n = int(input())
    M = [[0 for i in range(n)] for j in range(n)]
    for _ in range(n):
        l = list(map(int, input().split(" ")))
        u = l[0]
        k = l[1]
        if k != 0:
            for v in l[2:]:
                M[u - 1][v - 1] = 1

    color = [0] * n
    d = [-1] * n
    Q = deque()

    def bfs(s):  # breadth-first search
        color[s] = 1
        d[s] = 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] = 1
                    d[v] = d[u] + 1
                    Q.append(v)
            color[u] = 2

    bfs(0)
    for i in range(n):
        print(i + 1, d[i])
 from collections import deque if __name__ == '__main__':

    n = int(input())
    M = [[0 for i in range(n)] for j in range(n)]
    for _ in range(n):
        l = list(map(int, input().split(" ")))
        u = l[0]
        k = l[1]
        if k != 0:
            for v in l[2:]:
                M[u - 1][v - 1] = 1

    color = [0] * n
    d = [-1] * n
    Q = deque()

    def bfs(s):  # breadth-first search
        color[s] = 1
        d[s] = 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] = 1
                    d[v] = d[u] + 1
                    Q.append(v)
            color[u] = 2

    bfs(0)
    for i in range(n):
        print(i + 1, d[i]) __name__ == '__main__' __name__ '__main__' 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)] for _ in range(n):
        l = list(map(int, input().split(" ")))
        u = l[0]
        k = l[1]
        if k != 0:
            for v in l[2:]:
                M[u - 1][v - 1] = 1

     _ range(n) range n l = list(map(int, input().split(" "))) l list(map(int, input().split(" "))) list map(int, input().split(" ")) map int input().split(" ") ().split () input split " " u = l[0] u l[0] l 0 k = l[1] k l[1] l 1 if k != 0:
            for v in l[2:]:
                M[u - 1][v - 1] = 1

     k != 0 k 0 for v in l[2:]:
                M[u - 1][v - 1] = 1

     v l[2:] l 2: 2 M[u - 1][v - 1] = 1 M[u - 1][v - 1] [u - 1] M u - 1 u 1 v - 1 v 1 1 color = [0] * n color [0] * n [0] 0 n d = [-1] * n d [-1] * n [-1] -1 n Q = deque() Q deque() deque def bfs(s):  # breadth-first search
        color[s] = 1
        d[s] = 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] = 1
                    d[v] = d[u] + 1
                    Q.append(v)
            color[u] = 2

     bfs color[s] = 1 color[s] color s 1 d[s] = 0 d[s] d 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] = 1
                    d[v] = d[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] = 1
                    d[v] = d[u] + 1
                    Q.append(v)
             v range(n) range n if M[u][v] == 1 and color[v] == 0:
                    color[v] = 1
                    d[v] = d[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] = 1 color[v] color v 1 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] = 2 color[u] color u 2 s s bfs(0) bfs 0 for i in range(n):
        print(i + 1, d[i]) i range(n) range n print(i + 1, d[i]) print i + 1 i 1 d[i] d i Q = deque() deque() Q color = [0] * n [0] * n color k = l[1] l[1] k d = [-1] * n [-1] * n d l = list(map(int, input().split(" "))) list(map(int, input().split(" "))) l 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):  # breadth-first search
        color[s] = 1
        d[s] = 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] = 1
                    d[v] = d[u] + 1
                    Q.append(v)
            color[u] = 2

     def bfs(s):  # breadth-first search
        color[s] = 1
        d[s] = 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] = 1
                    d[v] = d[u] + 1
                    Q.append(v)
            color[u] = 2

     bfs u = l[0] l[0] u n = int(input()) int(input()) n
# -*- coding: utf-8 -*-
from collections import deque


if __name__ == '__main__':

    n = int(input())
    M = [[0 for i in range(n)] for j in range(n)]
    for _ in range(n):
        l = list(map(int, input().split(" ")))
        u = l[0]
        k = l[1]
        if k != 0:
            for v in l[2:]:
                M[u - 1][v - 1] = 1

    color = [0] * n
    d = [-1] * n
    Q = deque()

    def bfs(s):  # breadth-first search
        color[s] = 1
        d[s] = 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] = 1
                    d[v] = d[u] + 1
                    Q.append(v)
            color[u] = 2

    bfs(0)
    for i in range(n):
        print(i + 1, d[i])

