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 <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
	int check_id;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
	check_id = 0;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == this->u) return 0;

	if (id == check_id) return -1;
	else check_id = id;

	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d + 1, d_min);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

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

const int white = 0, gray = 1, black = 2, N = 101, INF = -1;
bool G[N][N];
int n, color[N], d[N];

void init(){
    for (int i = 1; i <= n; i++)
    {
        color[i] = white;
        d[i] = INF;
    }
}

void bfs(int v){
    queue<int> q;
    q.push(v);
    d[v] = 0;
    int u;
    while (!q.empty())
    {
        u = q.front(); q.pop();
        for (int i = 1; i <= n; i++)
        {
            if (G[u][i] && color[i] == white)
            {
                d[i] = d[u] + 1;
                q.push(i);
                color[i] = gray;
            }
        }
    color[u] = black;
    }
}

int main(){
    int u, k, v;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> u >> k;
        for (int j = 1; j <= k; j++)
        {
            cin >> v;
            G[u][v] = 1;
        }
    }
    init();
    bfs(1);
    for (int i = 1; i <= n; i++)
    {
        cout << i << " " << d[i] << endl;
    }   
}

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

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

	int G[100][100]={0};

	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;
		}
	}

	int d[100];
	for(int i=0; i<n; i++){
		d[i] = -1;
	}
	queue<int> Q;

	Q.push(0);
	d[0] = 0;

	while(!Q.empty()){
		int u = Q.front();
		int v = -1;
		for(int j=0; j<n; j++){
			if(G[u][j]==1 && d[j]==-1){
				v = j;
				break;
			}
		}

		if(v!=-1){
			Q.push(v);
			d[v] = d[u]+1;
		}else{
			Q.pop();
		}
	}
	
	for(int i=0; i<n; i++){
		cout << i+1 << ' ' << d[i] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
int main() {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    rep(i, n) {
        int u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    vector<int> d(n, -1);
    queue<int> q;
    q.push(0);
    d[0] = 0;
    while (sz(q)) {
        int now = q.front();
        q.pop();
        for (int nxt : g[now]) {
            if (d[nxt] != -1) continue;
            d[nxt] = d[now] + 1;
            q.push(nxt);
        }
    }
    rep(i, n) cout << i + 1 << " " << d[i] << endl;
}