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 <queue>
using namespace std;
queue<int> qu;
int v[101][101];
int dis[101], n;
int d = 0;
int tf;
inline void b1(int t) {
	for (int i = 1; i <= n; i++) {
		if (v[t][i]) {
			tf = 1;
			qu.push(i);
			dis[i] = d + 1;
			for (int j = 1; j <= n; j++)
				v[j][i] = 0;
		}
	}
}
inline void b2() {
	tf = 0;
	for (int i = 1; i <= n; i++)
		if (dis[i] == d) {
			b1(i);
			qu.pop();
		}
	d++;
}
inline void bfs() {
	tf = 1;
	b1(1);
	d++;
	qu.pop();
	while (tf) {
		b2();
	}
}
int main() {
	int u, k, s;
	cin >> n;
	for (int i = 2; i <= n; i++) {
		dis[i] = -1;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			v[i][j] = 0;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u;
		cin >> k;
		for (int j = 0; j < k; j++) {
			cin >> s;
			v[u][s] = 1;
		}
	}
	qu.push(1);
	for (int i = 1; i <= n; i++)
		v[i][1] = 0;
	bfs();
	for (int i = 1; i <= n; i++) {
		cout << i << " " << dis[i] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int n;
vector<int> a[100];
int dist[100];
const int INF = 100000000;

int main() {
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    rep(i, y) {
      int z;
      cin >> z;
      a[x-1].push_back(z-1);
    }
  }

    rep(i, n) {
      dist[i] = INF;
    }
    dist[0] = 0;

    queue<int> que;

    que.push(0);
    while(que.size()) {
      int p = que.front();
      que.pop();

      for(int i = 0; i < a[p].size(); i++) {
        if(dist[a[p][i]] > dist[p] + 1) {
          dist[a[p][i]] = dist[p] + 1;
          que.push(a[p][i]);
        }
      }
    }

  rep(i, n) {
    if(dist[i] != INF) cout << i + 1 << " " << dist[i] << endl;
    else cout << i + 1 << " " << -1 << endl;
  }
    

  return 0;
}

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

static const int N = 100;
static const int INF = (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] = INF;
	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] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((d[i] == INF) ? (-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--; // 0オリジン
		for (int j = 0; j < k; j++) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}

	bfs(0);
	
	return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
int main() {
    int V, k, source, target, current, next;
    bool oneVisited;
    scanf("%d", &V);
    vector<int> G[V+1];
    bool visited[V+1];
    int disc[V+1];
    fill(visited, visited+V+1, 0);
    fill(disc, disc+V+1, -1);
    for (int i = 0; i < V; i++) {
        scanf("%d %d", &source, &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &target);
            G[source].push_back(target);
        }
    }
    queue<int> q;
    q.push(1);
    int d = 0; disc[1] = 0;
    visited[1] = true;
    while (!q.empty()) {
        current = q.front(); q.pop();
        for (int i = 0; i < G[current].size(); i++) {
            next = G[current][i];
            if (!visited[next]) {
                visited[next] = true;
                disc[next] = disc[current]+1;
                q.push(next);
            }
        }
    }
    for (int i = 1; i <= V; i++) printf("%d %d\n", i, disc[i]);
    return 0;
}

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

#define M 1000000007
vector<int> dist;
vector<vector<int>> edge;

int main() {
    int n;
    cin >> n;
    dist.resize(n, M);
    edge.resize(n);
    for (int i = 0; i < n; ++i) {
        int id, k;
        cin >> id >> k;
        if (id != i + 1) exit(1);
        if (!k) continue;

        edge[i].resize(k);
        for (int j = 0; j < k; ++j) {
            cin >> edge[i][j];
            --edge[i][j];
        }
    }
    queue<int> q;
    q.push(0);
    dist[0] = 0;
    while (!q.empty()) {
        int p = q.front();
        q.pop();
        for (int t : edge[p]) {
            if (dist[t] > dist[p] + 1) {
                dist[t] = dist[p] + 1;
                q.push(t);
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << (i + 1) << " " << (dist[i] < M ? dist[i] : -1) << "\n";
    }
    return 0;
}