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<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
#include<iterator>
#include<list>
using namespace std;
typedef  long long int lint;
//typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#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 reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)
#define mp make_pair
#define P pair<int,int>
int main() {
	int INF = 10000000;
	int V;
	cin >> V;
	vector<vector<int>>G(V);
	rep(i, V) {
		int v, k;
		cin >> v >> k;
		v--;
		rep(j, k) {
			int tmp;
			cin >> tmp;
			tmp--;
			G[v].push_back(tmp);
		}
	}
	int start = 0;//スタート位置

	vector<int>dist(V, INF);//startからの距離を入れる配列。INF(未訪問)で初期化
	queue<int> que;

	dist[start] = 0;
	que.push(start);

	//BFS
	while (!que.empty()) {
		int v = que.front();
		que.pop();

		for (int nv : G[v]) {
			if (dist[nv] != INF)continue;
			dist[nv] = dist[v] + 1;
			que.push(nv);
		}
	}
	rep(i, V) {
		if (dist[i] == INF) {
			cout << i + 1 << ' ' << -1 << endl;
		}
		else {
			cout << i + 1 << ' ' << dist[i] << endl;
		}
	}
}

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


#define show(x) #x " = " << x

int n;
vector<int> G[100];
int d[100];

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

	queue<int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int v = q.front(); q.pop();
		for (int i = 0; i < G[v].size(); i++) {
			int x = G[v][i];
			if (d[x] == -1) {
				d[x] = d[v] + 1;
				q.push(x);
			}
		}
	}
	for (i = 0; i < n; i++) cout << i + 1 << " " << d[i] << endl;

}

C++: #include <bits/stdc++.h>
using namespace std;

#define N_MAX 100
#define WH 0
#define BK 2


long n;
long G[N_MAX][N_MAX] = {};
long color[N_MAX] = {};
long dist[N_MAX];
queue<long> Q;

void bfs(long u) {
    Q.push(u);
    color[u] = BK;
    dist[u] = 0;
    while (Q.empty() == false) {
        u = Q.front();
        Q.pop();
        for (long v = 0; v < n; v++) {
            if (G[u][v] == 1 && color[v] == WH) {
                Q.push(v);
                color[v] = BK;
                dist[v] = dist[u] + 1;
            }
        }
    }
}

int main() {
    long v_from, deg_out, v_to;

    cin >> n;
    for (long i = 0; i < n; i++) {
        cin >> v_from >> deg_out;
        v_from--;
        for (long j = 0; j < deg_out; j++) {
            cin >> v_to;
            v_to--;
            G[v_from][v_to] = 1;
        }
    }

    // for (long i = 0; i < n; i++) {
    //     for (long j = 0; j < n; j++) {
    //         if (j != 0) { cout << " "; }
    //         cout << G[i][j];
    //     }
    //     cout << endl;
    // }


    for (long i = 0; i < n; i++) {
        dist[i] = -1;
    }

    bfs(0);

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

    return 0;
}

C++: #include<bits/stdc++.h>
#define MAX 100
using namespace std;
vector<int>adjlist[MAX+5];
int visited[MAX+5];
int dist[MAX+5];
int main()
{
    int n,x,u,k,y,g;
    cin>>n;
    memset(dist,-1,sizeof(dist));
    for(int i=1; i<=n; i++)
    {
        cin>>u>>k;
        for(int j=0; j<k; j++)
        {
            cin>>x;
            adjlist[u].push_back(x);
        }
    }
    queue<int>q;
        int i=1;
        dist[i]=0;
        if(visited[i]==0 && adjlist[i].size()!=0)
        {
            //cout<<i<<endl;
            q.push(i);
            visited[i]=1;
            while(!q.empty())
            {
                x=q.front();
                q.pop();
                for(int j=0; j<adjlist[x].size(); j++)
                {
                    g=adjlist[x][j];
                    if(visited[g]==0)
                    {
                        visited[g]=1;
                        dist[g]=dist[x]+1;
                        q.push(g);
                    }

                }
            }
        }

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

    return 0;

}