Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Pair struct {
	v int
	d int
}

type Queue []Pair

func (q *Queue) push(v Pair) {
	*q = append(*q, v)
}

func (q *Queue) pop() Pair {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>

using namespace std;

int main(){
        vector<int> v[1005];
        bool used[1005];
        int dis[1005];
        memset(used, 0, sizeof(used));
        memset(dis, -1, sizeof(dis));
        int N ; cin >> N;
        for(int j = 0; j < N; j++){
            int id, t, tmp; cin >> id >> t;
            for (int i = 0; i < t; i++){
                cin >> tmp;
                v[id].push_back(tmp);
                //v[tmp].push_back(id);
            }
        }
        queue<int> q;
        q.push(1);
        used[1] = 1;
        dis[1] = 0;
        while(!q.empty()){
            int k = q.front();
            q.pop();
            for (int x : v[k]){
                if(!used[x]){
                    q.push(x);
                    dis[x] = dis[k] + 1;
                    used[x] = 1;
                }
            }
        }
        for (int i = 1; i <= N; i++)
            cout << i << " " << dis[i] << '\n';

}

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

using Graph = std::vector<std::vector<int>>;

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

    Graph g(n);
    for(int i = 0; i < n; ++i){
        int u, k;
        std::cin >> u >> k;
        --u;
        for(int j = 0; j < k; ++j){
            int v;
            std::cin >> v;
            --v;
            g.at(u).push_back(v);            
            //g.at(v).push_back(u); 
        }
    }

    std::vector<int> dist(n, -1);
    std::queue<int> queue;
    dist.at(0) = 0;
    queue.push(0);

    while(!queue.empty()){
        int v = queue.front();
        queue.pop();

        for(auto nextV : g.at(v)){
//            if(-1 != dist.at(nextV)){
//                continue;
//            }
//            queue.push(nextV);
//            dist.at(nextV) = dist.at(v) + 1;
            if(-1 == dist.at(nextV)){
                queue.push(nextV);
                dist.at(nextV) = dist.at(v) + 1;
            }
        }
    }

    for(int i = 0; i < n; ++i){
        std::cout << i + 1 << " " << dist.at(i) << std::endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  rep(i,n){
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
       d[i][j]=100000;
      }
    }
  }
  rep(i,n){
    int u,k;
    cin >> u >> k;
    q1.push(u);
    if(k>0){
     int v[k];
     rep(j,k){
       cin >> v[j];
       q1.push(v[j]);
     }
     int f = q1.front()-1;
     q1.pop();
     rep(i,k){
       int e=q1.front()-1;
       d[f][e]=1;
       q1.pop();
     }
    }else{
     q1.pop();
    }
  }

  rep(i,n){
    for(j=2;j<n;j++){
    d[0][i]=min(d[0][i],d[0][j]+d[j][i]);
    d[0][i]=min(d[0][i],d[0][j]+d[i][j]);
    }
  }
 
 rep(i,n){
   cout << i+1 << ' ';
   if(d[0][i]==100000){
     cout << -1 << endl;
   }else{
    cout << d[0][i] << endl;
   }
 }
 
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
ll dx[4] = { 0, 1, 0, -1 };
ll dy[4] = { 1, 0, -1, 0 };
const ll INF = 1 << 21;
const ll MOD = 1e9 + 7;
static const int N = 100;

int n;
int M[N][N];
int d[N]; // 距離で訪問状態を管理する

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    rep(i, n)
    {
        d[i] = INF;
    }
    d[s] = 0;
    int u;
    while (!q.empty()) {
        u = q.front();
        q.pop();
        rep(v, n)
        {
            if (M[u][v] == 0) { // u->vに行けないなら飛ばす
                continue;
            }
            if (d[v] != INF) { // d[v]に行ったことがあるなら飛ばす
                continue;
            }
            d[v] = d[u] + 1; // u->vが始めてなら距離を更新
            q.push(v); // queueにvを入れる
        }
    }
    rep(i, n)
    {
        cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int u, k, v;
    cin >> n;
    rep(i, n)
    {
        rep(j, n)
        {
            M[i][j] = 0;
        }
    }
    rep(i, n)
    {
        cin >> u >> k;
        u--;
        rep(j, k)
        {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0); // 0から出発

    return 0;
}

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

int n,m,k,l,a[101][101],d[101],t;
queue<int> q;

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>l>>m;
        for(int j=0;j<m;j++){
            cin>>k;
            a[l][k]=1;
        }
        d[i+1]=-1;
    }
    d[1]=0;
    q.push(1);
    while(!q.empty()){
        t=q.front();
        q.pop();
        for(int i=1;i<=n;i++){
            if(a[t][i]){
                if(d[i]==-1){
                    d[i]=d[t]+1;
                    q.push(i);
                }
            }
        }
    }
    for(int i=1;i<=n;i++){cout<<i<<' '<<d[i]<<endl;}
}