Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

Python: import sys
n = int(input())
input_line = [[int(i) for i in str.split()] for str in sys.stdin.read().splitlines()]
for i in range(n):
    input_line[i] = [] if len(input_line[i]) == 2 else input_line[i][2:]
is_visit = [False] * n
distance = [0] * n
queue = [1]

while len(queue) != 0:
    id = queue.pop(0)
    is_visit[id-1] = True
    for node_id in input_line[id-1]:
        if not is_visit[node_id-1]:
            distance[node_id-1] = distance[id-1] + 1
            queue.append(node_id)
            is_visit[node_id-1] = True

for i in range(n):
    d = distance[i] if is_visit[i] else -1
    print('{0} {1}'.format(i+1, d))

Python: from collections import deque
N = int(input())
edge = [[] for _ in range(N+1)]
for i in range(N):
  A = list(map(int, input().split()))
  edge[A[0]] = A[2:]

ans = [-1]*N
ans[0] = 0
d = deque([[1,0]])
while len(d)>0:
  v,dist = d.popleft()
  for w in edge[v]:
    if ans[w-1]==-1:
      ans[w-1]=dist+1
      d.append([w,dist+1])
for i in range(N):
  print(i+1,ans[i])

Python: from collections import deque
def bfs(s):
    color[s] = 'GRAY'
    d[s] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == 'WHITE':
                color[v] = 'GRAY'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'BLACK'
        
n = int(input())
M = [[0] * n for _ in range(n)]
color = ['WHITE'] * n
d = [-1] * n
Q = deque()

for _ in range(n):
    a = list(map(int, input().split()))
    u = a[0]
    v = a[2:]
    for j in v:
        M[u - 1][j - 1] = 1
        
bfs(0)

for i in range(n):
    out = ''
    out += '{} {}'.format(i + 1, d[i])
    print(out)

Python: N= int(input())
E = [input().split()[2:] for _ in [0]*N]
Q = [0]
L = [0] + [-1]*N
def func(ide):
    global Q
    for i in E[ide]:
        i = int(i) - 1
        if L[i] < 0 :L[i] = L[ide] +1 ;Q.append(i)
    

while Q:top = Q.pop(0);func(top)

for i in range(N):print(i + 1 ,int(L[i]))

Python: n = int(input())

adj_list = [[] for _ in range(n + 1)]

for i in range(n):
    tmp = list(map(int, input().split()))
    u = tmp[0]
    k = tmp[1]
    for v in range(2, 2 + k):
        adj_list[u].append(tmp[v])

d = [-1] * (n + 1)
q = []
q.append(1)
d[1] = 0

while len(q) > 0:
    u = q.pop(0)
    for v in adj_list[u]:
        if d[v] == -1:
            d[v] = d[u] + 1
            q.append(v)

for idx, d in enumerate(d):
    if idx != 0:
        print("%d %d" % (idx, d))

Python: import queue
def s():
 N=int(input())
 M=[input().split()[2:]for _ in[0]*N]
 q=queue.Queue();q.put(0)
 d=[-1]*N;d[0]=0
 while not q.empty():
  u=q.get()
  for v in M[u]:
   v=int(v)-1
   if d[v]<0:d[v]=d[u]+1;q.put(v)
 for i in range(N):print(i+1,d[i])
if'__main__'==__name__:s()

Python: from collections import deque
n = int(input())
A = [0]*(n+1)
D = [-1]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
d.append(1)
D[1] = 0
DP[1] = 1
while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i)
for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp)

Python: N= int(input())
E = [input().split()[2:] for _ in range(N)]
Q = [0]
L = [0] + [-1]*N
frag = [1] + [0]*N
def func(ide):
    global Q
    for i in E[ide]:
        i = int(i) - 1
        if frag[i] != 1:frag[i] = 1;L[i] = L[ide] +1 ;Q.append(i)
    

while Q:top = Q[0];func(top);del Q[0]

for i in range(N):print(i + 1 ,int(L[i]))

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

def BFS(adj, start):
    n = len(adj)
    d = [100] * 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
    
    return d

def main():
    n = int(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()

Python: from collections import deque
N = int(input())
graph = [list(map(int, input().split())) for _ in range(N)]
seen = [-1]*(N+1)

todo = deque()


def bfs(v):
    u = v-1
    next_v = graph[u][2:]
    for i in next_v:
        if seen[i] == -1:
            seen[i] = seen[v] + 1
            todo.append(i)


todo.append(1)
seen[1] = 0

while len(todo) > 0:
    bfs(todo.popleft())

for i in range(1, N+1):
    print(i, seen[i])

Python: from queue import *

class Vertex:
	def __init__(self, u, k):
		self.u = u
		self.vertices = [0] * k
		self.d = -1
	def __str__(self):
		return str(self.u) + ' ' + str(self.d)

class Inf:
	def __init__(self,u,d):
		self.u = u
		self.d = d

def BFS(n, vertices):
	q = Queue()
	q.put(Inf(1,0))
	while q.empty() == False:
		inf = q.get()
		vertex = vertices[inf.u-1]
		if vertex.d == -1:
			vertex.d = inf.d
		else:
			vertex.d = min(vertex.d, inf.d)
		dist = inf.d + 1
		for i in range(len(vertex.vertices)):
			u = vertex.vertices[i]
			if vertices[u-1].d == -1:
				q.put(Inf(u, dist))
	
n = int(input())
vertices = []
for i in range(n):
	v = list(map(int, input().split()))
	u = v[0]
	k = v[1]
	vertex = Vertex(u, k)
	for j in range(k):
		vertex.vertices[j] = v[j+2]
	vertices.append(vertex)

BFS(n, vertices)
for i in range(n):
	print(str(vertices[i]))

Python: #coding:utf-8
        
def BFS(i,A,t):
    if i==1:
        return t
    else:
        for j in range(len(A)):
            if i in A[j]:
                return(BFS(j+1,A,t+1))
        return -1
            

if __name__=="__main__":
    n=int(input())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split(" ")))[2:])
    d=[0]*n
    t=0
    for i in range(n):
        d[i]=BFS(i+1,A,t)
    for i in range(n):
        print(i+1,d[i])