Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

var infty = 1000000000

type nodeKey int

type DirectedGraphNode struct {
	key       nodeKey
	adjacents []*DirectedGraphNode
	visited   bool
	d         int
	parent    *DirectedGraphNode
}

type DirectedGraph struct {
	nodes []DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(l, r nodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, &g.nodes[r])
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]DirectedGraphNode, nNodes)
	for iNodes := 0; iNodes < nNodes; iNodes++ {
		graph.nodes[iNodes].key = nodeKey(iNodes)

		graph.nodes[iNodes].visited = false
		graph.nodes[iNodes].d = infty
		graph.nodes[iNodes].parent = nil
	}
	return &graph
}

type QueueEntry struct {
	node *DirectedGraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *DirectedGraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

func (q *Queue) isEmpty() bool {
	return q.head == nil
}

func fillDistancesByBreadthFirstSearch(g *DirectedGraph, startNode *DirectedGraphNode) {
	startNode.visited = true
	startNode.d = 0

	queue := Queue{}
	queue.enqueue(startNode)
	for !queue.isEmpty() {
		u := queue.dequeue().node
		for _, adjacent := range u.adjacents {
			if !adjacent.visited {
				adjacent.visited = true
				adjacent.d = u.d + 1
				adjacent.parent = u
				queue.enqueue(adjacent)
			}
		}
		u.visited = true
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	g := newDirectedGraph(nNodes + 1)

	for iNode := 0; iNode < nNodes; iNode++ {
		nodeKeyLeft := nodeKey(nextInt())
		nAdj := nextInt()
		for iAdj := 0; iAdj < nAdj; iAdj++ {
			nodeKeyRight := nodeKey(nextInt())
			g.setAdjacency(nodeKeyLeft, nodeKeyRight)
		}
	}

	// fmt.Println(g)
	fillDistancesByBreadthFirstSearch(g, &g.nodes[1])
	// fmt.Println(g)

	for iNode := 1; iNode <= nNodes; iNode++ {
		node := g.nodes[iNode]
		distance := node.d
		if distance == infty {
			distance = -1
		}
		fmt.Println(node.key, distance)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define sortr(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define max 100005
#define nil -1

constexpr int inf = 1000000;
vector<vector<int>> g;
vector<int> d;

void bfs(int s){
    d.resize(max);
   fill(d.begin(),d.end(),inf);
   d[s]=0;
   queue<int> q;
   q.push(s);
   while(!q.empty()){
       int now=q.front();
       q.pop();
       for(auto &e:g[now]){
           if(d[e]==inf){
               d[e]=d[now]+1;
               q.push(e);
           }
       }
   }
}

int main() {
  int  n,u,k,v;
  cin>>n;
    g.resize(n);
    d.resize(n);
  rep(i,n){
      cin>>u>>k;
      u--;
      for(int j=0;j<k;j++){
          cin>>v;
          v--;
          g[u].push_back(v);
        }
    }
    bfs(0);
    rep(i,n){
        if(d[i]<inf){
            cout<<i+1<<" "<<d[i]<<endl;
        }else{
            cout<<i+1<<" "<<-1<<endl;
        }
    }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;
using Graph = vector<vector<int>>;

int main() {
	int n;
	cin >> n;

	Graph G(n);

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			v--;
			G[u].push_back(v);
		}
	}

	vector<int> dist(n, -1);
	queue<int> q;
	q.push(0);
	dist[0] = 0;

	while (!q.empty()) {
		int v = q.front();
		q.pop();

		for (int nv : G[v]) {
			if (dist[nv] != -1) continue;
			dist[nv] = dist[v] + 1;
			q.push(nv);
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << dist[i] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i ++) d[i] = INFTY;
  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front(); q.pop();
    for (int v = 0; v < n; v ++) {
      if (M[u][v] == 0) continue;
      if (d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i ++) {
    cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;

  for (int i = 0; i < n; i ++) {
    for (int j = 0; j < n; j ++) M[i][j] = 0;
  }

  for (int i = 0; i < n; i ++) {
    cin >> u >> k;
    u --;
    for (int j = 0; j < k; j ++) {
      cin >> v;
      v --;
      M[u][v] = 1;
    }
  }

  bfs(0);
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int n=0;//頂点数 ○
int u=0;//頂点の番号　○
int k[101]={0};//頂点番号の値　○
int v[101][101]={0};//??
int d[101]={0};//shortest ○
int fptr=0;//デキューをするための変数　○
int lptr=0;//エンキューをするための変数　○
int u1[10000];//頂点の番号　○
int d1[10000];//頂点への最短距離　○
int id=0;//i==id.　○
void enqueue(int u2,int d2)
{
	u1[lptr]=u2;//頂点の番号はu2
	d1[lptr]=d2;//頂点への最短距離はd2
	lptr=(lptr+1)%10000;//今何番目の配列か求める
}
void bfs(void)
{
	int u3;
	int d3;

	while(fptr!=lptr)
	{
		u3=u1[fptr];//頂点番号代入
		d3=d1[fptr];//最短距離代入
		fptr=(fptr+1)%10000;
		if(d[u3]!=-1)
		{
			continue;
		}
		for(int i=0;i<k[u3];i++)
		{
			enqueue(v[u3][i],d3+1);
		}
		d[u3]=d3;
	}
	/*
	bfsの引数は二つ→それぞれをu1[fptr]、d1[fptr]に代入
	1.whileで全体を回す（条件付）
	2.デキューをする（whileの直後）
	3.最短距離が更新されてなければcontinue（条件付）	
	4.enqueue（for文で回す）
	*/
}
int main(void)
{	
	scanf("%d",&n);//頂点数を決める
	for(int i=0;i<n;i++)//0が頂点数になるまで
	{
		scanf("%d",&u);//各頂点の隣接リスト入力 1:u=%d...%d(n):u=%d
		scanf("%d",&k[u]);//配列ｋ[u]に値を入れる 1:k[1]=%d...k[u]=%d
		for(int j=0;j<k[u];j++)//j<k[u]のとき
		{
			scanf("%d",&v[u][j]);//配列ｖ[頂点の番号][]に値を入れる
		}
	}
	enqueue(1,0);
	for(int i=0;i<n+1;i++)//全ての値を未探索にする
	{
		d[i]=-1;//未探索の値
	}
	bfs();
	for(int i=1;i<=n;i++)
	{
	printf("%d %d\n",i,d[i]);
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;

queue<int> Q;
int n,u,k,v,tem;
int D[110];
vector<int> edges[110];
void solve() {
    D[1] = 0;
    while (! Q.empty()) {
        int cur = Q.front();
        Q.pop();
        while(edges[cur].size()){
            tem=edges[cur].back();
            edges[cur].pop_back();
            if(D[tem]==-1){
                Q.push(tem);
                D[tem]=D[cur]+1;
            }
        }
    }
        for (int i=1;i<=n;i++) {
            cout<<i<<' '<<D[i]<<endl;
        }
}


int main(){
    for(int i=0;i<110;i++){
        D[i]=-1;
    }
    cin>>n;
    for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
        cin>>v;
        edges[u].push_back(v);
    }
}
    Q.push(1);
    solve();
        return 0;
}