n = int(input())
 
adj = [None]
for i in range(n):
    adji = list(map(int, input().split()[2:]))
    adj.append(adji)
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 ce in q:
            for ne in adj[ce]:
                if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d
BFS(1)
for i, x in enumerate(distance[1:], start=1):
    print(i, x) n = int(input()) n int(input()) int input() input adj = [None] adj [None] None for i in range(n):
    adji = list(map(int, input().split()[2:]))
    adj.append(adji) i range(n) range n adji = list(map(int, input().split()[2:])) adji list(map(int, input().split()[2:])) list map(int, input().split()[2:]) map int input().split()[2:] ().split() ().split () input split 2: 2 adj.append(adji) adj.append adj append adji isSearched = [None] + [False] * n isSearched [None] + [False] * n [None] None [False] * n [False] False n distance = [None] + [-1] * n distance [None] + [-1] * n [None] None [-1] * n [-1] -1 n def BFS(u):
    d = 0
    isSearched[u] = True
    distance[u] = d
    edge = [u]
    while edge:
        q = list(edge)
        edge = []
        d += 1
        for ce in q:
            for ne in adj[ce]:
                if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d BFS d = 0 d 0 isSearched[u] = True isSearched[u] isSearched u True distance[u] = d distance[u] distance u d edge = [u] edge [u] u while edge:
        q = list(edge)
        edge = []
        d += 1
        for ce in q:
            for ne in adj[ce]:
                if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d edge q = list(edge) q list(edge) list edge edge = [] edge [] d += 1 d 1 for ce in q:
            for ne in adj[ce]:
                if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d ce q for ne in adj[ce]:
                if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d ne adj[ce] adj ce if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d not isSearched[ne] isSearched[ne] isSearched ne isSearched[ne] = True isSearched[ne] isSearched ne True edge.append(ne) edge.append edge append ne distance[ne] = d distance[ne] distance ne d u u BFS(1) BFS 1 for i, x in enumerate(distance[1:], start=1):
    print(i, x) i x enumerate(distance[1:], start=1) enumerate distance[1:] distance 1: 1 start=1 start 1 print(i, x) print i x def BFS(u):
    d = 0
    isSearched[u] = True
    distance[u] = d
    edge = [u]
    while edge:
        q = list(edge)
        edge = []
        d += 1
        for ce in q:
            for ne in adj[ce]:
                if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d def BFS(u):
    d = 0
    isSearched[u] = True
    distance[u] = d
    edge = [u]
    while edge:
        q = list(edge)
        edge = []
        d += 1
        for ce in q:
            for ne in adj[ce]:
                if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d BFS adji = list(map(int, input().split()[2:])) list(map(int, input().split()[2:])) adji distance = [None] + [-1] * n [None] + [-1] * n distance n = int(input()) int(input()) n adj = [None] [None] adj isSearched = [None] + [False] * n [None] + [False] * n isSearched
n = int(input())
 
adj = [None]
for i in range(n):
    adji = list(map(int, input().split()[2:]))
    adj.append(adji)
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 ce in q:
            for ne in adj[ce]:
                if not isSearched[ne]:
                    isSearched[ne] = True
                    edge.append(ne)
                    distance[ne] = d
BFS(1)
for i, x in enumerate(distance[1:], start=1):
    print(i, x)