INF = float('inf')

v = int(input())
graph = [None]

for i in range(v):
    u = list(map(int, input().split()))
    
    graph.append(u[2:])

visited = [False] * (v+1)
visited[1] = True
cost = [INF] * (v+1)
cost[1] = 0
c = 0

q = graph[1]

while q:
    c += 1
    q_next = []
    for u in q:
        if not visited[u]:
            visited[u] = True
            cost[u] = c
            q_next.extend(graph[u])
    q = q_next
    
for i in range(v):
    if cost[i+1] == INF: cost[i+1] = -1
    print(i+1, cost[i+1]) INF = float('inf') INF float('inf') float 'inf' v = int(input()) v int(input()) int input() input graph = [None] graph [None] None for i in range(v):
    u = list(map(int, input().split()))
    
    graph.append(u[2:]) i range(v) range v u = list(map(int, input().split())) u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split graph.append(u[2:]) graph.append graph append u[2:] u 2: 2 visited = [False] * (v+1) visited [False] * (v+1) [False] False v+1 v 1 visited[1] = True visited[1] visited 1 True cost = [INF] * (v+1) cost [INF] * (v+1) [INF] INF v+1 v 1 cost[1] = 0 cost[1] cost 1 0 c = 0 c 0 q = graph[1] q graph[1] graph 1 while q:
    c += 1
    q_next = []
    for u in q:
        if not visited[u]:
            visited[u] = True
            cost[u] = c
            q_next.extend(graph[u])
    q = q_next
     q c += 1 c 1 q_next = [] q_next [] for u in q:
        if not visited[u]:
            visited[u] = True
            cost[u] = c
            q_next.extend(graph[u])
     u q if not visited[u]:
            visited[u] = True
            cost[u] = c
            q_next.extend(graph[u])
     not visited[u] visited[u] visited u visited[u] = True visited[u] visited u True cost[u] = c cost[u] cost u c q_next.extend(graph[u]) q_next.extend q_next extend graph[u] graph u q = q_next q q_next for i in range(v):
    if cost[i+1] == INF: cost[i+1] = -1
    print(i+1, cost[i+1]) i range(v) range v if cost[i+1] == INF: cost[i+1] = -1
     cost[i+1] == INF cost[i+1] cost i+1 i 1 INF print(i+1, cost[i+1]) print i+1 i 1 cost[i+1] cost i+1 i 1 visited = [False] * (v+1) [False] * (v+1) visited q = graph[1] graph[1] q q_next = [] [] q_next q = q_next q_next q u = list(map(int, input().split())) list(map(int, input().split())) u INF = float('inf') float('inf') INF cost = [INF] * (v+1) [INF] * (v+1) cost graph = [None] [None] graph c = 0 0 c c += 1 1 c v = int(input()) int(input()) v
INF = float('inf')

v = int(input())
graph = [None]

for i in range(v):
    u = list(map(int, input().split()))
    
    graph.append(u[2:])

visited = [False] * (v+1)
visited[1] = True
cost = [INF] * (v+1)
cost[1] = 0
c = 0

q = graph[1]

while q:
    c += 1
    q_next = []
    for u in q:
        if not visited[u]:
            visited[u] = True
            cost[u] = c
            q_next.extend(graph[u])
    q = q_next
    
for i in range(v):
    if cost[i+1] == INF: cost[i+1] = -1
    print(i+1, cost[i+1])
