# -*- coding: utf-8 -*-                                                                                                                                                                                                                               



def BFS(adj, start):

    n = len(adj)

    d = [n] * n

    flag = [0] * n

    Q = []



    Q.append(start)

    d[start] = 0



    while len(Q) != 0:

        u = Q.pop(0)

        v = [i for i, x in enumerate(adj[u]) if (x == 1) and (flag[i] == 0)] # v <= ??£??\???????????????????????¢?´¢??????????????????

        

        for i in v:

            Q.append(i)

            d[i] = min(d[u] + 1, d[i])

            flag[u] = 1

    

    for i in range(n):

        if d[i] == n:

            d[i] = -1



    return d



def main():

    n = int(eval(input()))

    adj = [[0 for i in range(n)] for j in range(n)]

    

    for i in range(n):

        tmp = list(map(int, input().split()))

        u = tmp[0]

        u = u -1

        k = tmp[1]

        v = tmp[2:]

        for j in range(k):

            adj[u][v[j]-1] = 1

    

    d = BFS(adj, 0)

    for i in range(n):

        print((i+1, d[i]))





if __name__ == "__main__":

    main()
n = int(eval(input()))

 

adj = [None]

for i in range(n):

    adj_i = list(map(int, input().split()[2:]))

    adj.append(adj_i)

 

isSearched = [None] + [False] * n

 

distance = [None] + [-1] * n

 

def bfs(u):

    d = 0

    isSearched[u] = True

    distance[u] = d

    edge = [u]

    while edge:

        q = list(edge)

        edge = []

        d += 1

        for c_e in q:

            for n_e in adj[c_e]:

                if not isSearched[n_e]:

                    isSearched[n_e] = True

                    edge.append(n_e)

                    distance[n_e] = d

 

 

bfs(1)

 

for i, x in enumerate(distance[1:], start=1):

    print((i, x))
# -*- coding: utf-8 -*-
def BFS(adj, start):
    n = len(adj)
    d = [n] * n
    flag = [0] * n
    Q = []
    Q.append(start)
    d[start] = 0
    while len(Q) != 0:
        u = Q.pop(0)
        v = [
            i for i, x in enumerate(adj[u]) if (x == 1) and (flag[i] == 0)
        ]  # v <= ??£??\???????????????????????¢?´¢??????????????????
        for i in v:
            Q.append(i)
            d[i] = min(d[u] + 1, d[i])
            flag[u] = 1
    for i in range(n):
        if d[i] == n:
            d[i] = -1
    return d


def main():
    n = int(eval(input()))
    adj = [[0 for i in range(n)] for j in range(n)]
    for i in range(n):
        tmp = list(map(int, input().split()))
        u = tmp[0]
        u = u - 1
        k = tmp[1]
        v = tmp[2:]
        for j in range(k):
            adj[u][v[j] - 1] = 1
    d = BFS(adj, 0)
    for i in range(n):
        print((i + 1, d[i]))


if __name__ == "__main__":
    main()

n = int(eval(input()))
adj = [None]
for i in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i)
isSearched = [None] + [False] * n
distance = [None] + [-1] * n


def bfs(u):
    d = 0
    isSearched[u] = True
    distance[u] = d
    edge = [u]
    while edge:
        q = list(edge)
        edge = []
        d += 1
        for c_e in q:
            for n_e in adj[c_e]:
                if not isSearched[n_e]:
                    isSearched[n_e] = True
                    edge.append(n_e)
                    distance[n_e] = d


bfs(1)
for i, x in enumerate(distance[1:], start=1):
    print((i, x))

-# -*- coding: utf-8 -*- -def BFS(adj, start): -    n = len(adj) -    d = [n] * n -    flag = [0] * n -    Q = [] -    Q.append(start) -    d[start] = 0 -    while len(Q) != 0: -        u = Q.pop(0) -        v = [ -            i for i, x in enumerate(adj[u]) if (x == 1) and (flag[i] == 0) -        ]  # v <= ??£??\???????????????????????¢?´¢?????????????????? -        for i in v: -            Q.append(i) -            d[i] = min(d[u] + 1, d[i]) -            flag[u] = 1 -    for i in range(n): -        if d[i] == n: -            d[i] = -1 -    return d +n = int(eval(input())) +adj = [None] +for i in range(n): +    adj_i = list(map(int, input().split()[2:])) +    adj.append(adj_i) +isSearched = [None] + [False] * n +distance = [None] + [-1] * n -def main(): -    n = int(eval(input())) -    adj = [[0 for i in range(n)] for j in range(n)] -    for i in range(n): -        tmp = list(map(int, input().split())) -        u = tmp[0] -        u = u - 1 -        k = tmp[1] -        v = tmp[2:] -        for j in range(k): -            adj[u][v[j] - 1] = 1 -    d = BFS(adj, 0) -    for i in range(n): -        print((i + 1, d[i])) +def bfs(u): +    d = 0 +    isSearched[u] = True +    distance[u] = d +    edge = [u] +    while edge: +        q = list(edge) +        edge = [] +        d += 1 +        for c_e in q: +            for n_e in adj[c_e]: +                if not isSearched[n_e]: +                    isSearched[n_e] = True +                    edge.append(n_e) +                    distance[n_e] = d -if __name__ == "__main__": -    main() +bfs(1) +for i, x in enumerate(distance[1:], start=1): +    print((i, x))