Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

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)
        else:return

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

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

Python: from queue import Queue

n = int(input())
A = [[0 for i in range(n)] for j in range(n)]
d = [-1] * n

for i in range(n):
    u, k, *v = list(map(int, input().split()))
    for j in v:
        A[int(u)-1][int(j)-1] = 1

q = Queue()
q.put(0)
d[0] = 0

while not q.empty():

    u = q.get()

    for v in range(n):

        if A[u][v] == 1 and d[v] == -1:
            q.put(v)
            d[v] = d[u] + 1

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

Python: from collections import deque

n = int(input())
adj = [list(map(int, input().split(' '))) for i in range(n)]

class Node:
  def __init__(self, id, ck):
    self.id = id
    self.ck = ck
    self.depth = -1

  def __str__(self):
    return '{} {}'.format(self.id, self.depth)

class Graph:
  def __init__(self):
    self.map = {}

  def get(self, id):
    return self.map[id]

  def set(self, id, ck):
    self.map[id] = Node(id, ck)

  def bfs(self):
    root = self.get(1)
    root.depth = 0
    queue = deque()
    queue.append(root)
    while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

  def __str__(self):
    return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))]))

graph = Graph()
for data in adj:
  id = data[0]
  ck = data[2:]
  graph.set(id, ck)

graph.bfs()
print(graph)

Python: n = int(input())
Adj = [[0 for i in range(n)] for i in range(n)]
 
for i in range(n):
    u = list(map(int, input().split()))
    if u[1] > 0:
        for i in u[2: 2 + u[1]]:
            Adj[u[0] - 1][i - 1] = 1
 
color = [0] * n
d = [-1] * n
import queue
Q = queue.Queue()

color[0] = 1
d[0] = 0
Q.put(1)
while not Q.empty():
    u = Q.get()
    for v in range(1, n+1):
        if Adj[u-1][v-1] == 1 and color[v-1] == 0:
            color[v-1] = 1
            d[v-1] = d[u-1] + 1
            Q.put(v)
    color[u-1] = 2

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

Python: 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)

Python: #Breadth First Search
n=int(input())
V=[]
a=[1]
for i in range(n):
    V.append([int(i) for i in input().split()])
    a.append(0)
V=sorted(V,key=lambda x:x[0])
print('1 0')
d=1
c=0
w=[0]
p=[]
while c<n and w!=[]:
    i=0
    y=[]
 #   print(c)
    while c<n and i<len(w):
        j=2
        while j<2+V[w[i]][1] and c<n:
            if a[V[w[i]][j]-1]==0:
        #        print(a)
                a[V[w[i]][j]-1]=1
                p.append([V[w[i]][j],d])
                c+=1
                y.append(V[w[i]][j]-1)
            j+=1
        i+=1
    w=y
    d+=1
p=sorted(p, key=lambda x:x[0])
#print(p)
i=2
j=0
while i <=n and j<len(p):
    while p[j][0]>i:
        print(str(i) +" -1")
        i+=1
    print(str(p[j][0])+" "+str(p[j][1]))
    i+=1
    j+=1
while i<=n:
    print(str(i)+" -1")
    i+=1

Python: def bfs(G, s):
    d = {key: -1 for key in G.keys()}
    d[s] = 0
    Q = [s]
    while len(Q) > 0:
        u = Q.pop(0)
        for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

    return d


if __name__ == '__main__':
    G = {}
    n = int(input())
    for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        G[u] = tmp
    d = bfs(G, 1)
    for node in G:
        print('{0} {1} '.format(node, d[node]))

Python: import queue

n = int(input())
graph = {}
for _ in range(n):
  i = list(map(int,input().split()))
  graph[i[0]] = i[2:]

dist = [-1 for i in range(n)]
todo = queue.Queue()
seen = set()

dist[0] = 0
todo.put(1)

while (not todo.empty()):
  now = todo.get()
  for nx in graph[now]:
    if (dist[nx-1] != -1):
      continue
    dist[nx-1] = dist[now-1] + 1
    todo.put(nx)

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

Python: from collections import deque

n = int(input())
adj = [[]]
for i in range(n):
    adj.append(list(map(int, input().split()))[2:])

ans = [-1]*(n+1)
ans[1] = 0

q = deque([1])
visited = [False] * (n+1)
visited[1] = True

while q:
    x = q.popleft()
    for y in adj[x]:
        if visited[y] == False:
            q.append(y)
            ans[y] = ans[x]+1
            visited[y] = True

for j in range(1, n+1):
    print(j, ans[j])

Python: n = int(input())
adj = [list(map(int, input().split()))[2:] for _ in range(n)]
 
d = [-1] * n
 
s = 1
q = []
d[s - 1] = 0
q.append(s)
while len(q):
    u = q.pop()
    v = adj[u - 1]
    for vi in v:
        if d[vi - 1] == -1:
            d[vi - 1] = d[u - 1] + 1
            q.append(vi)
 
 
for i, di in enumerate(d):
    print(i + 1, di)

Python: from collections import deque
n = int(input())

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

q = deque()
q.append(1)

checked = [False] * (n + 1)
checked[1] = True
d = [-1] * (n + 1)
d[1] = 0

while q:
    current = q.pop()
    for v in G[current]:
        if not checked[v]:
            q.append(v)
            d[v] = d[current] + 1
            checked[v] = True

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

Python: n = int(input())
d = [-1]*(n+1)
adjacent_list = [[] for _ in range(n+1)]
for _ in range(n):
    edge = list(map(int,input().split()))
    if len(edge) >= 3:
        adjacent_list[edge[0]].extend(edge[2:])

from collections import deque

que = deque()
que.append(1)
d[1] = 0
while len(que) != 0:
    v = que.popleft()
    for nv in adjacent_list[v]:
        if d[nv] == -1:
            d[nv] = d[v] + 1
        else:
            continue
        que.append(nv)

for i,dist in enumerate(d):
    if i == 0:
        continue
    print(i,dist)