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;
#define mod 1000000007
#define MAX 105
#define BLACK 0
#define GRAY 1
#define WHITE 2

int n;
int G[MAX][MAX];
int d[MAX], color[MAX];

void init();
void bfs();

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

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

    bfs();

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

void init(){
    for(int i = 0; i < n; i++){
        d[i] = -1;
        color[i] = WHITE;
        for(int j = 0; j < n; j++){
            G[i][j] = 0;
        }
    }
}

void bfs(){
    queue<int> Q;
    int now = 0;
    color[now] = GRAY; d[now] = 0;
    Q.push(now);

    while (!Q.empty()){
        for(int i = 0; i < n; i++){
            if(color[i] == WHITE && G[now][i] == 1){
                Q.push(i); color[i] = GRAY; d[i] = d[now]+1;
            }
        }
        Q.pop(); now = Q.front(); color[now] = BLACK;
    }

}

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

using namespace std;

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

// 幅優先探索
// 最短距離を求める。

struct Node
{
    // 頂点 0オリジン
    int id;
    // 頂点1からの距離。たどり着けない場合はINFTY
    int direction;
};

int M[MAX][MAX] = { 0 };
Node nodes[MAX];

void initNodes(Node *nodes, int len)
{
    for (int i = 0; i < len; i++)
    {
        nodes[i].id = i;
        nodes[i].direction = INFTY;
    }
    // nodes[0]は頂点1で、ここから開始するのでdirecton=0にする
    nodes[0].direction = 0;
}

// s 始点0オリジン
// n 頂点数
void depthFirstSearch(int s, int n)
{
    queue<Node> q;
    q.push(nodes[s]);

    while (!q.empty())
    {
        // c++のqueueはpopで値を返さない。先頭の参照を取得後、popで削除。
        Node uNode = q.front(); q.pop();
        // ここで訪問完了

        for (int v = 0; v < n; v++)
        {
            if (M[uNode.id][v] == 0) continue;     // 行き先がない。
            if (nodes[v].direction != INFTY) continue;   // すでに到達しているのでスキップ

            // u->vで、頂点uが保持している距離+1をnodes[v]に入れる
            nodes[v].direction = nodes[uNode.id].direction + 1;

            // 頂点uに隣接するvがここでキューに入る。
            // for文なので、このループで隣接する頂点がキューに入る。
            q.push(nodes[v]);
        }
    }
}

void printNodes(Node *nodes, int n)
{
    for (int i = 0; i < n; i++)
    {
        int val = nodes[i].direction == INFTY ? -1 : nodes[i].direction;
        cout << nodes[i].id + 1 << " " << val << endl;
    }
}

int main(int argc, char *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int u, k;
        cin >> u; // 頂点番号
        cin >> k; // 次数
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            M[u-1][v-1] = 1;
        }
    }

    initNodes(nodes, MAX);
    depthFirstSearch(0, n); //始点0 = 頂点1から探索開始
    printNodes(nodes, n);
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

static const int N = 100;
int d[N];
int n,M[N][N];

void dfs() {
	queue<int> q;
	q.push(0);
	int i;
	rep(i, n) d[i] = INF;
	d[0] = 0;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		int v;
		rep(v, n) {
			if (M[u][v] == 0) continue;
			if (d[v] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	rep(u, n) {
		cout << u + 1 << ' ' << ((d[u] == INF) ? (-1) : d[u]) << '\n';
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin >> n;
	int i;
	rep(i, n) {
		int u, k, v;
		cin >> u >> k;
		u--;
		int j;
		rep(j, k) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	dfs();
	return 0;
}

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

struct VRTX {
	int v[101] = {}, dist = -1;
};

int n, curdist = 0;
VRTX u[101];

void breadthFirstSearch(int x, int dist);

int main() {
	int i, j, id, dim;
	
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> id >> dim;
		for (j = 0; j < dim; j++) {
			cin >> u[id].v[j];
		}
	}

	breadthFirstSearch(1, 0);

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

	return 0;
}

void breadthFirstSearch(int x, int dist) {
	int i, nextdist;

	if (u[x].dist == -1)u[x].dist = dist;
	else if (u[x].dist <= dist)return;
	else u[x].dist = dist;
	
	i = 0;
	nextdist = u[x].dist + 1;
	while (u[x].v[i] != 0) {
		breadthFirstSearch(u[x].v[i], nextdist);
		i++;
	}
}