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>
#define MOD 1000000007
#define INF 1<<30
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define RREP(i,a,b) for(ll i=(a); i>=(b); i--)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef int64_t in;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef pair<ll, ll> p;
typedef vector<pair<ll, ll>> vpl;

vvl m(105, vl(105,0));
vl d(105, INF);
ll n;

void bfs(ll s){
    queue<ll> q;
    q.push(s);
    d[s] = 0;
    ll u;
    while(!q.empty()){
        u = q.front(); q.pop();
        rep(v,n){
            if(m[u][v] == 1){
                if(d[v] == INF){
                    d[v] = d[u] + 1;
                    q.push(v);
                }
            }
        }
    }
    rep(i,n){
        if(d[i] != INF) cout << i+1 << " " << d[i] << endl; 
        else cout << i+1 << " " << -1 << endl;
    }
}

int main(){
    ll u,k,v;
    cin >> n;
    rep(i,n){
        cin >> u >> k;
        rep(j,k){
            cin >> v;
            m[u-1][v-1] = 1;
        }
    }
    bfs(0);
    
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

int main(){
   int n,u,v,k,inf = -1;
   cin >> n;
   vector<int> a(n,0),D(n,inf);
   vector<vector<int> > edges(n,a); 
   for(int i = 0;i < n;i++){
       cin >> u;
       cin >> v;
       for(int ii = 0;ii < v;ii++){
           cin >> k;
           edges[u-1][k-1] = 1;
       }
   }
   queue<int> Q;
   Q.push(0);
   D[0] = 0;
   while(!Q.empty()){
        int cur = Q.front();
        Q.pop();//最初の要素を削除、詰める
        for(int dst = 0;dst < n;dst++){
            if(edges[cur][dst] == 1 && D[dst] == inf){
                D[dst] = D[cur] + 1;
                Q.push(dst);
            }
        }
   }
   for(int i = 0;i < n;i++){
       cout << i+1 << " " << D[i] << endl;
   }
}

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

queue<pair<int,int> > qu;
vector<int> v[111];
int root;
int b[111];
int d[111];
pair<int, int> pa;
int g;

void wide(int num){

  memset(d,-1,sizeof(d));
  d[num] = 0;
  qu.push(make_pair(num,d[1]));

  while(!qu.empty()){

    qu.front();
    pa = qu.front();
    qu.pop();
    int id = pa.first;

    for(int i = 0; i < (int)v[id].size(); i++){
      if(d[v[id][i]] == -1){
	d[v[id][i]] = pa.second + 1;
	qu.push(make_pair(v[id][i],d[v[id][i]]));
      }
    }
  }
  for(int i = 1; i <= g; i++){
    cout << i << " " << d[i] << endl;
  }

}

int main(){

 
  cin >> g;
  int n;
  for(int i = 0; i < g; i++){
    cin >> n >> b[i];
    for(int j = 0; j < b[i]; j++){
      cin >> root;
      v[n].push_back(root);
      v[root].push_back(n);
    }
  }

  wide(1);

}

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

static const int N=100;

int n,M[N][N];
int d[N];

using namespace std;
void bfs(int s)
{
    queue<int> q;
    q.push(s);
    for(int i=0;i<n;i++) d[i]=INT_MAX;
    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]!=INT_MAX) continue;
            d[v]=d[u]+1;
            q.push(v);
         }
    }
    for(int i=0;i<n;i++)
    {
        cout<<i+1<<" "<<((d[i]==INT_MAX)?(-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--;
        for(int j=0;j<k;j++)
        {
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define ALL(vec) vec.begin(), vec.end()
#define rALL(vec) vec.rbegin(), vec.rend()
using namespace std;
using ll = long long;

vector<int> d, color;
vector<vector<int>> A;

void bfs() {
    queue<int> q;
    q.push(0);
    d[0] = 0;
    color[0] = 1;
    while (!q.empty()) {
        int i = q.front(); q.pop();
        for (auto j : A[i]) {
            if (color[j] == 0) {
                q.push(j);
                d[j] = d[i] + 1;
                color[j] = 1;
            }
        }
    }
}

int main() {
    int n;
    cin >> n;
    A = vector<vector<int>>(n, vector<int>(0));
    d = vector<int>(n, -1);
    color = vector<int>(n, 0);
    rep(i, n) {
        int u, k;
        cin >> u >> k;
        rep(j, k) {
            int tmp;
            cin >> tmp;
            A[i].push_back(--tmp);
        }
    }
    bfs();
    rep(i, n) {
        printf("%d %d\n", i + 1, d[i]);
    }
    return 0;
}