n = int(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) n = int(input()) n int(input()) int input() input adj = [None] adj [None] None for i in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i) i range(n) range n adj_i = list(map(int, input().split()[2:])) adj_i 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(adj_i) adj.append adj append adj_i 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 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 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 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 edge q = list(edge) q list(edge) list edge edge = [] edge [] d += 1 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 c_e 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 n_e adj[c_e] adj c_e if not isSearched[n_e]:
                    isSearched[n_e] = True
                    edge.append(n_e)
                    distance[n_e] = d not isSearched[n_e] isSearched[n_e] isSearched n_e isSearched[n_e] = True isSearched[n_e] isSearched n_e True edge.append(n_e) edge.append edge append n_e distance[n_e] = d distance[n_e] distance n_e 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 adj_i = list(map(int, input().split()[2:])) list(map(int, input().split()[2:])) adj_i distance = [None] + [-1] * n [None] + [-1] * n distance n = int(input()) int(input()) n isSearched = [None] + [False] * n [None] + [False] * n isSearched adj = [None] [None] adj 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 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
n = int(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)