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 unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
constexpr ll MOD = ll(1e9 + 7);
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(1e5) + 5;
constexpr double EPS = 1e-9;
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()

int main() {
    vector<vector<int> > g;
    int n;
    cin >> n;
    g.resize(n);
    for(int i=0;i<n;i++){
        int v, k;
        cin >> v >> k;
        v--;
        for(int j=0;j<k;j++){
            int u;
            cin >> u;
            u--;
            g[v].push_back(u);
        }
    }
    vector<int> d(n, IINF);
    queue<int> que;
    que.push(0);
    d[0] = 0;
    que.push(0);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        for(auto u : g[v]){
            if(d[u] == IINF){
                d[u] = d[v]+1;
                que.push(u);
            }
        }
    }
    for(int i=0;i<n;i++){
        cout << i+1 << " " << (d[i] < IINF ? d[i] : -1) << endl;
    }
    return 0;
}

C++: # include <bits/stdc++.h>
# define pb push_back
# define int long long
using namespace std;
void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); }
#define Cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define Cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i]
#define Cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i]
#define Cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<int>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]
void Print() { cout << endl; }
template <class Head, class... Tail> void Print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; Print(forward<Tail>(tail)...); }
template <class Type> void Print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class Type> void Print(vector<vector<Type>> &df) { for (auto& vec : df) { Print(vec); } }
void Debug() { cerr << endl; }
template <class Head, class... Tail> void Debug(Head&& head, Tail&&... tail) { cerr << head; if (sizeof...(tail) != 0) cerr << " "; Debug(forward<Tail>(tail)...); }
template <class Type> void Debug(vector<Type> &vec) { for (auto& a : vec) { cerr << a; if (&a != &vec.back()) cerr << " "; } cerr << endl; }
template <class Type> void Debug(vector<vector<Type>> &df) { for (auto& vec : df) { Debug(vec); } }
vector<int> range(int n) {
    vector<int> ar(max((int)0, n));
    iota(ar.begin(), ar.end(), 0);
    return ar;
}

signed main() {
    Cin(int, n); // 頂点数
    vector<vector<int>> G(n);
    for (auto i : range(n)) {
        Cin(int, u, k);
        u--;
        for (auto j : range(k)) {
            Cin(int, v);
            v--;
            G[u].pb(v);
        }
    }

    vector<int> dist(n, -1);
    queue<int> que;
    que.push(0);
    dist[0] = 0;
    while (!que.empty()) {
        int now = que.front(); que.pop();
        for (auto nxt : G[now]) {
            if (dist[nxt] != -1) continue;
            dist[nxt] = dist[now] + 1;
            que.push(nxt);
        }
    }

    for (auto i : range(n)) {
        cout << i+1 << " " << dist[i] << "\n";
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i< (n);i++)
using namespace std;
typedef long long ll;

struct BFS{
    vector<vector<int>> adj;
    queue<int> Q;
    vector<int> d;
    int n_;
    BFS(int n):adj(n),d(n,-1){
        n_ = n;
    }

    void init(){
        rep(i,n_){
            int u,k;cin>>u>>k;u--;
            rep(i,k){
                int tmp;cin>>tmp;tmp--;
                adj[u].push_back(tmp);
            }
        }
    }

    void visit(int u){
        Q.push(u);
        d[u] = 0;
        while(!Q.empty()){
            int v = Q.front();Q.pop();
            for(int h:adj[v]){
                if(d[h] == -1){
                    Q.push(h);
                    d[h] = d[v] + 1;
                }
            }
        }
    }
    void solve(){
        init();
        visit(0);
        rep(i,n_){
            cout<<i+1<<" "<<d[i]<<endl;
        }
    }

};

int main(){
    int n;cin>>n;
    BFS bfs(n);
    bfs.solve();
    return 0;
}

C++: #include <bits/stdc++.h>
#include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int a[101][101],start[101],end[101],n,p=0,v[101];
queue<int> q;
void bfs(int u){
	q.push(u);
	v[u]++;
	while(!q.empty()){
		u=q.front();q.pop();
		r(i,1,n+1)if(!v[i]&&a[u][i]){
			v[i]++;
			q.push(i);
			start[i]=start[u]+1;
		}
	}
}
int main(){
	int q,qq,qqq;
	cin>>n;
	r(i,0,n){
		cin>>q>>qq;
		r(i,0,qq){
			cin>>qqq;
			a[q][qqq]++;
		}
	}
	r(i,1,n+1)if(!v[i])
	bfs(i);
	r(i,1,n+1)cout<<i<<' '<<start[i]<<endl;
}