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


class Graph:
    def __init__(self, n, adj_matrix):
        self.n = n
        self.adj_matrix = adj_matrix

    def show(self):
        for _ in self.adj_matrix:
            print(*_)

    def dfs(self):
        self.states = [2] * self.n
        self.d = [None] * self.n
        self.f = [None] * self.n
        self.time = 0

        for u in range(self.n):
            if self.states[u] == 2:
                self.dfs_visit(u)

    def dfs_visit(self, u):
        self.states[u] = 1
        self.time += 1
        self.d[u] = self.time

        for v, edge in [(v, edge) for v, edge in enumerate(self.adj_matrix[u])
                        if edge]:
            if self.states[v] == 2:
                self.dfs_visit(v)

        self.states[u] = 0
        self.time += 1
        self.f[u] = self.time

    def show_dfs(self, start=0):
        self.dfs()
        for i, (d, f) in enumerate(zip(self.d, self.f)):
            print(i + 1, d, f)

    def bfs(self, start):
        self.states = [0] * self.n
        self.dists = [float('inf')] * self.n

        queue = deque([start])
        self.dists[start] = 0
        while queue:
            u = queue.popleft()
            edges = self.adj_matrix[u]

            for v, edge in [(v, edge) for v, edge in enumerate(edges) if edge]:
                if self.states[v]:
                    continue

                if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                queue.append(v)

                self.states[v] = 1

        self.dists = [
            -1 if dist == float('inf') else dist for dist in self.dists
        ]

    def show_bfs(self, start=0):
        self.bfs(start=start)
        for i, dist in enumerate(self.dists):
            print(i + 1, dist)


if __name__ == '__main__':
    n = int(input())
    adj_matrix = [[0] * n for i in range(n)]
    for i in range(n):
        for v in input().split()[2:]:
            adj_matrix[i][int(v) - 1] = 1

    g = Graph(n, adj_matrix)
    g.show_bfs()
 from collections import deque class Graph:
    def __init__(self, n, adj_matrix):
        self.n = n
        self.adj_matrix = adj_matrix

    def show(self):
        for _ in self.adj_matrix:
            print(*_)

    def dfs(self):
        self.states = [2] * self.n
        self.d = [None] * self.n
        self.f = [None] * self.n
        self.time = 0

        for u in range(self.n):
            if self.states[u] == 2:
                self.dfs_visit(u)

    def dfs_visit(self, u):
        self.states[u] = 1
        self.time += 1
        self.d[u] = self.time

        for v, edge in [(v, edge) for v, edge in enumerate(self.adj_matrix[u])
                        if edge]:
            if self.states[v] == 2:
                self.dfs_visit(v)

        self.states[u] = 0
        self.time += 1
        self.f[u] = self.time

    def show_dfs(self, start=0):
        self.dfs()
        for i, (d, f) in enumerate(zip(self.d, self.f)):
            print(i + 1, d, f)

    def bfs(self, start):
        self.states = [0] * self.n
        self.dists = [float('inf')] * self.n

        queue = deque([start])
        self.dists[start] = 0
        while queue:
            u = queue.popleft()
            edges = self.adj_matrix[u]

            for v, edge in [(v, edge) for v, edge in enumerate(edges) if edge]:
                if self.states[v]:
                    continue

                if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                queue.append(v)

                self.states[v] = 1

        self.dists = [
            -1 if dist == float('inf') else dist for dist in self.dists
        ]

    def show_bfs(self, start=0):
        self.bfs(start=start)
        for i, dist in enumerate(self.dists):
            print(i + 1, dist) Graph def __init__(self, n, adj_matrix):
        self.n = n
        self.adj_matrix = adj_matrix

     __init__ self.n = n self.n self n n self.adj_matrix = adj_matrix self.adj_matrix self adj_matrix adj_matrix self self n n adj_matrix adj_matrix def show(self):
        for _ in self.adj_matrix:
            print(*_)

     show for _ in self.adj_matrix:
            print(*_)

     _ self.adj_matrix self adj_matrix print(*_) print *_ _ self self def dfs(self):
        self.states = [2] * self.n
        self.d = [None] * self.n
        self.f = [None] * self.n
        self.time = 0

        for u in range(self.n):
            if self.states[u] == 2:
                self.dfs_visit(u)

     dfs self.states = [2] * self.n self.states self states [2] * self.n [2] 2 self.n self n self.d = [None] * self.n self.d self d [None] * self.n [None] None self.n self n self.f = [None] * self.n self.f self f [None] * self.n [None] None self.n self n self.time = 0 self.time self time 0 for u in range(self.n):
            if self.states[u] == 2:
                self.dfs_visit(u)

     u range(self.n) range self.n self n if self.states[u] == 2:
                self.dfs_visit(u)

     self.states[u] == 2 self.states[u] self.states self states u 2 self.dfs_visit(u) self.dfs_visit self dfs_visit u self self def dfs_visit(self, u):
        self.states[u] = 1
        self.time += 1
        self.d[u] = self.time

        for v, edge in [(v, edge) for v, edge in enumerate(self.adj_matrix[u])
                        if edge]:
            if self.states[v] == 2:
                self.dfs_visit(v)

        self.states[u] = 0
        self.time += 1
        self.f[u] = self.time

     dfs_visit self.states[u] = 1 self.states[u] self.states self states u 1 self.time += 1 self.time self time 1 self.d[u] = self.time self.d[u] self.d self d u self.time self time for v, edge in [(v, edge) for v, edge in enumerate(self.adj_matrix[u])
                        if edge]:
            if self.states[v] == 2:
                self.dfs_visit(v)

         v edge [(v, edge) for v, edge in enumerate(self.adj_matrix[u])
                        if edge] if self.states[v] == 2:
                self.dfs_visit(v)

         self.states[v] == 2 self.states[v] self.states self states v 2 self.dfs_visit(v) self.dfs_visit self dfs_visit v self.states[u] = 0 self.states[u] self.states self states u 0 self.time += 1 self.time self time 1 self.f[u] = self.time self.f[u] self.f self f u self.time self time self self u u def show_dfs(self, start=0):
        self.dfs()
        for i, (d, f) in enumerate(zip(self.d, self.f)):
            print(i + 1, d, f)

     show_dfs self.dfs() self.dfs self dfs for i, (d, f) in enumerate(zip(self.d, self.f)):
            print(i + 1, d, f)

     i (d, f) d f enumerate(zip(self.d, self.f)) enumerate zip(self.d, self.f) zip self.d self d self.f self f print(i + 1, d, f) print i + 1 i 1 d f self self start=0 start 0 def bfs(self, start):
        self.states = [0] * self.n
        self.dists = [float('inf')] * self.n

        queue = deque([start])
        self.dists[start] = 0
        while queue:
            u = queue.popleft()
            edges = self.adj_matrix[u]

            for v, edge in [(v, edge) for v, edge in enumerate(edges) if edge]:
                if self.states[v]:
                    continue

                if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                queue.append(v)

                self.states[v] = 1

        self.dists = [
            -1 if dist == float('inf') else dist for dist in self.dists
        ]

     bfs self.states = [0] * self.n self.states self states [0] * self.n [0] 0 self.n self n self.dists = [float('inf')] * self.n self.dists self dists [float('inf')] * self.n [float('inf')] float('inf') float 'inf' self.n self n queue = deque([start]) queue deque([start]) deque [start] start self.dists[start] = 0 self.dists[start] self.dists self dists start 0 while queue:
            u = queue.popleft()
            edges = self.adj_matrix[u]

            for v, edge in [(v, edge) for v, edge in enumerate(edges) if edge]:
                if self.states[v]:
                    continue

                if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                queue.append(v)

                self.states[v] = 1

         queue u = queue.popleft() u queue.popleft() queue.popleft queue popleft edges = self.adj_matrix[u] edges self.adj_matrix[u] self.adj_matrix self adj_matrix u for v, edge in [(v, edge) for v, edge in enumerate(edges) if edge]:
                if self.states[v]:
                    continue

                if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                queue.append(v)

                self.states[v] = 1

         v edge [(v, edge) for v, edge in enumerate(edges) if edge] if self.states[v]:
                    continue

                 self.states[v] self.states self states v continue if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                 self.dists[v] > self.dists[u] + 1 self.dists[v] self.dists self dists v self.dists[u] + 1 self.dists[u] self.dists self dists u 1 self.dists[v] = self.dists[u] + 1 self.dists[v] self.dists self dists v self.dists[u] + 1 self.dists[u] self.dists self dists u 1 queue.append(v) queue.append queue append v self.states[v] = 1 self.states[v] self.states self states v 1 -1 if dist == float('inf') else dist for dist in self.dists for dist in self.dists dist self.dists self dists for dist in self.dists -1 if dist == float('inf') else dist dist == float('inf') dist float('inf') float 'inf' -1 dist self.dists = [
            -1 if dist == float('inf') else dist for dist in self.dists
        ] self.dists self dists [
            -1 if dist == float('inf') else dist for dist in self.dists
        ] self self start start def show_bfs(self, start=0):
        self.bfs(start=start)
        for i, dist in enumerate(self.dists):
            print(i + 1, dist) show_bfs self.bfs(start=start) self.bfs self bfs start=start start start for i, dist in enumerate(self.dists):
            print(i + 1, dist) i dist enumerate(self.dists) enumerate self.dists self dists print(i + 1, dist) print i + 1 i 1 dist self self start=0 start 0 if __name__ == '__main__':
    n = int(input())
    adj_matrix = [[0] * n for i in range(n)]
    for i in range(n):
        for v in input().split()[2:]:
            adj_matrix[i][int(v) - 1] = 1

    g = Graph(n, adj_matrix)
    g.show_bfs() __name__ == '__main__' __name__ '__main__' 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 adj_matrix = [[0] * n for i in range(n)] adj_matrix [[0] * n for i in range(n)] for i in range(n):
        for v in input().split()[2:]:
            adj_matrix[i][int(v) - 1] = 1

     i range(n) range n for v in input().split()[2:]:
            adj_matrix[i][int(v) - 1] = 1

     v input().split()[2:] ().split() ().split () input split 2: 2 adj_matrix[i][int(v) - 1] = 1 adj_matrix[i][int(v) - 1] [i] adj_matrix i int(v) - 1 int(v) int v 1 1 g = Graph(n, adj_matrix) g Graph(n, adj_matrix) Graph n adj_matrix g.show_bfs() g.show_bfs g show_bfs g = Graph(n, adj_matrix) Graph(n, adj_matrix) g adj_matrix = [[0] * n for i in range(n)] [[0] * n for i in range(n)] adj_matrix class Graph:
    def __init__(self, n, adj_matrix):
        self.n = n
        self.adj_matrix = adj_matrix

    def show(self):
        for _ in self.adj_matrix:
            print(*_)

    def dfs(self):
        self.states = [2] * self.n
        self.d = [None] * self.n
        self.f = [None] * self.n
        self.time = 0

        for u in range(self.n):
            if self.states[u] == 2:
                self.dfs_visit(u)

    def dfs_visit(self, u):
        self.states[u] = 1
        self.time += 1
        self.d[u] = self.time

        for v, edge in [(v, edge) for v, edge in enumerate(self.adj_matrix[u])
                        if edge]:
            if self.states[v] == 2:
                self.dfs_visit(v)

        self.states[u] = 0
        self.time += 1
        self.f[u] = self.time

    def show_dfs(self, start=0):
        self.dfs()
        for i, (d, f) in enumerate(zip(self.d, self.f)):
            print(i + 1, d, f)

    def bfs(self, start):
        self.states = [0] * self.n
        self.dists = [float('inf')] * self.n

        queue = deque([start])
        self.dists[start] = 0
        while queue:
            u = queue.popleft()
            edges = self.adj_matrix[u]

            for v, edge in [(v, edge) for v, edge in enumerate(edges) if edge]:
                if self.states[v]:
                    continue

                if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                queue.append(v)

                self.states[v] = 1

        self.dists = [
            -1 if dist == float('inf') else dist for dist in self.dists
        ]

    def show_bfs(self, start=0):
        self.bfs(start=start)
        for i, dist in enumerate(self.dists):
            print(i + 1, dist) class Graph:
    def __init__(self, n, adj_matrix):
        self.n = n
        self.adj_matrix = adj_matrix

    def show(self):
        for _ in self.adj_matrix:
            print(*_)

    def dfs(self):
        self.states = [2] * self.n
        self.d = [None] * self.n
        self.f = [None] * self.n
        self.time = 0

        for u in range(self.n):
            if self.states[u] == 2:
                self.dfs_visit(u)

    def dfs_visit(self, u):
        self.states[u] = 1
        self.time += 1
        self.d[u] = self.time

        for v, edge in [(v, edge) for v, edge in enumerate(self.adj_matrix[u])
                        if edge]:
            if self.states[v] == 2:
                self.dfs_visit(v)

        self.states[u] = 0
        self.time += 1
        self.f[u] = self.time

    def show_dfs(self, start=0):
        self.dfs()
        for i, (d, f) in enumerate(zip(self.d, self.f)):
            print(i + 1, d, f)

    def bfs(self, start):
        self.states = [0] * self.n
        self.dists = [float('inf')] * self.n

        queue = deque([start])
        self.dists[start] = 0
        while queue:
            u = queue.popleft()
            edges = self.adj_matrix[u]

            for v, edge in [(v, edge) for v, edge in enumerate(edges) if edge]:
                if self.states[v]:
                    continue

                if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                queue.append(v)

                self.states[v] = 1

        self.dists = [
            -1 if dist == float('inf') else dist for dist in self.dists
        ]

    def show_bfs(self, start=0):
        self.bfs(start=start)
        for i, dist in enumerate(self.dists):
            print(i + 1, dist) Graph n = int(input()) int(input()) n
# -*- coding: utf-8 -*-
from collections import deque


class Graph:
    def __init__(self, n, adj_matrix):
        self.n = n
        self.adj_matrix = adj_matrix

    def show(self):
        for _ in self.adj_matrix:
            print(*_)

    def dfs(self):
        self.states = [2] * self.n
        self.d = [None] * self.n
        self.f = [None] * self.n
        self.time = 0

        for u in range(self.n):
            if self.states[u] == 2:
                self.dfs_visit(u)

    def dfs_visit(self, u):
        self.states[u] = 1
        self.time += 1
        self.d[u] = self.time

        for v, edge in [(v, edge) for v, edge in enumerate(self.adj_matrix[u])
                        if edge]:
            if self.states[v] == 2:
                self.dfs_visit(v)

        self.states[u] = 0
        self.time += 1
        self.f[u] = self.time

    def show_dfs(self, start=0):
        self.dfs()
        for i, (d, f) in enumerate(zip(self.d, self.f)):
            print(i + 1, d, f)

    def bfs(self, start):
        self.states = [0] * self.n
        self.dists = [float('inf')] * self.n

        queue = deque([start])
        self.dists[start] = 0
        while queue:
            u = queue.popleft()
            edges = self.adj_matrix[u]

            for v, edge in [(v, edge) for v, edge in enumerate(edges) if edge]:
                if self.states[v]:
                    continue

                if self.dists[v] > self.dists[u] + 1:
                    self.dists[v] = self.dists[u] + 1
                queue.append(v)

                self.states[v] = 1

        self.dists = [
            -1 if dist == float('inf') else dist for dist in self.dists
        ]

    def show_bfs(self, start=0):
        self.bfs(start=start)
        for i, dist in enumerate(self.dists):
            print(i + 1, dist)


if __name__ == '__main__':
    n = int(input())
    adj_matrix = [[0] * n for i in range(n)]
    for i in range(n):
        for v in input().split()[2:]:
            adj_matrix[i][int(v) - 1] = 1

    g = Graph(n, adj_matrix)
    g.show_bfs()

