def bfs(graph, start, result):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        unvisited = unvisited[1:]
        if visited[now] == None:
            result[now] = 0
        else:
            result[now] = result[visited[now]] + 1
        for next in graph[now][2:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return visited

n = int(input())
g = [[]]
r = {i: -1 for i in range(n + 1)}

for i in range(n):
    g.append(list(map(int, input().split())))

v = bfs(g, 1, r)

for i in range(1,n+1):
    print(i, r[i]) def bfs(graph, start, result):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        unvisited = unvisited[1:]
        if visited[now] == None:
            result[now] = 0
        else:
            result[now] = result[visited[now]] + 1
        for next in graph[now][2:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return visited bfs visited = {start: None} visited {start: None} start start None unvisited = [start] unvisited [start] start while unvisited:
        now = unvisited[0]
        unvisited = unvisited[1:]
        if visited[now] == None:
            result[now] = 0
        else:
            result[now] = result[visited[now]] + 1
        for next in graph[now][2:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
     unvisited now = unvisited[0] now unvisited[0] unvisited 0 unvisited = unvisited[1:] unvisited unvisited[1:] unvisited 1: 1 if visited[now] == None:
            result[now] = 0
        else:
            result[now] = result[visited[now]] + 1
         visited[now] == None visited[now] visited now None result[now] = 0 result[now] result now 0 result[now] = result[visited[now]] + 1 result[now] result now result[visited[now]] + 1 result[visited[now]] result visited[now] visited now 1 for next in graph[now][2:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
     next graph[now][2:] [now] graph now 2: 2 if not (next in visited):
                unvisited.append(next)
                visited[next] = now
     not (next in visited) next in visited next visited unvisited.append(next) unvisited.append unvisited append next visited[next] = now visited[next] visited next now return visited visited graph graph start start result result n = int(input()) n int(input()) int input() input g = [[]] g [[]] [] i: -1 for i in range(n + 1) for i in range(n + 1) i range(n + 1) range n + 1 n 1 for i in range(n + 1) i -1 r = {i: -1 for i in range(n + 1)} r {i: -1 for i in range(n + 1)} for i in range(n):
    g.append(list(map(int, input().split()))) i range(n) range n g.append(list(map(int, input().split()))) g.append g append list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split v = bfs(g, 1, r) v bfs(g, 1, r) bfs g 1 r for i in range(1,n+1):
    print(i, r[i]) i range(1,n+1) range 1 n+1 n 1 print(i, r[i]) print i r[i] r i r = {i: -1 for i in range(n + 1)} {i: -1 for i in range(n + 1)} r g = [[]] [[]] g def bfs(graph, start, result):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        unvisited = unvisited[1:]
        if visited[now] == None:
            result[now] = 0
        else:
            result[now] = result[visited[now]] + 1
        for next in graph[now][2:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return visited def bfs(graph, start, result):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        unvisited = unvisited[1:]
        if visited[now] == None:
            result[now] = 0
        else:
            result[now] = result[visited[now]] + 1
        for next in graph[now][2:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return visited bfs n = int(input()) int(input()) n v = bfs(g, 1, r) bfs(g, 1, r) v
def bfs(graph, start, result):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        unvisited = unvisited[1:]
        if visited[now] == None:
            result[now] = 0
        else:
            result[now] = result[visited[now]] + 1
        for next in graph[now][2:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return visited

n = int(input())
g = [[]]
r = {i: -1 for i in range(n + 1)}

for i in range(n):
    g.append(list(map(int, input().split())))

v = bfs(g, 1, r)

for i in range(1,n+1):
    print(i, r[i])