Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

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

const long N = 100;
const long INFTY = (1<<21);

long n, M[N + 1][N + 1];
long d[N + 1];

void bfs(long s){
	queue<long> q;
	q.push(s);
	for (long i = 1; i <= n; i++) d[i] = INFTY;
	d[s] = 0;
	long u;
	while (!q.empty()){
		u = q.front();
		q.pop();
		for (long v = 1; v <= n; v++){
			if (M[u][v] == 0) continue;
			if (d[v] != INFTY) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	for (long i = 1; i <= n; i++){
		cout << i << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
	}
}

int main(){
	long u, k, v;
	cin >> n;
	for (long i = 1; i <= n; i++){
		for (long j = 1; j <= n; j++){
			M[i][j] = 0;
		}
	}
	for (long i = 0; i < n; i++){
		cin >> u >> k;
		for (long j = 0; j < n; j++){
			cin >> v;
			M[u][v] = 1;
		}
	}

	bfs(1);

	return 0;
}

C++: #include "bits/stdc++.h"
 
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef priority_queue<int> PQ_DESC;
typedef priority_queue<int, vector<int>, greater<int>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;

#define SORT_ASC(c) sort((c).begin(), (c).end())
#define SORT_DESC(c) sort((c).begin(), (c).end(), greater<typeof((c).begin())>())
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define FORL(i,a,b) for(LL i=(a);i<(b);++i)
#define REPL(i,n)  FORL(i,0,n)
#define ALL(a) (a).begin(),(a).end()
 
const double EPS = 1e-10;
const double PI  = acos(-1.0);

 //debug func
template<typename T>
void vprint(vector<T> v) {
    for(auto x : v) {
        cerr << x << " ";
    }
    cerr << endl;
}
 
template<typename T>
void vvprint(vector<vector<T>> vv) {
    REP(i, vv.size()) {
        REP(j, vv[i].size()) {
            cerr << vv[i][j] << " ";
        }
        cerr << endl;
    }
}

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
{
   /* Credits: Thomas Draper */
   if ((first == last) || (first == k) || (last == k))
      return false;
   Iterator itr1 = first;
   Iterator itr2 = last;
   ++itr1;
   if (last == itr1)
      return false;
   itr1 = last;
   --itr1;
   itr1 = k;
   --itr2;
   while (first != itr1)
   {
      if (*--itr1 < *itr2)
      {
         Iterator j = k;
         while (!(*itr1 < *j)) ++j;
         iter_swap(itr1,j);
         ++itr1;
         ++j;
         itr2 = k;
         rotate(itr1,j,last);
         while (last != j)
         {
            ++j;
            ++itr2;
         }
         rotate(k,itr2,last);
         return true;
      }
   }
   rotate(first,k,last);
   return false;
}

inline double get_time_sec(void){
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count())/1000000000;
}

template<typename T>
T lcm(T a, T b) {
    return (a / __gcd(a, b)) * b;
}

template<typename T>
map<T, T> prime_list(T n) {
    map<T, T> ret;
    for(T i = 2; i*i <= n; i++) {
        if(n % i == 0) {
            ret[i] = 0;
            while(n % i == 0) {
                n /= i;
                ret[i]++;
            }
        }
    }
    if(n != 1) ret[n]++;
    return ret;
}

#define MOD 1000000007

LL mypow(LL a, LL n) {
    if(n == 0) return 1;
    if(n == 1) return a % MOD;
    if(n % 2 == 1) return (a * mypow(a, n-1)) % MOD;
    LL t = mypow(a, n/2);
    return (t * t) % MOD; 
}

LL mycomb(LL n, LL k) {
    if(n == 0 || n == k) return 1;
    LL x = 1, y = 1;
    REP(i, k) {
        x = x * (n-i) % MOD;
        y = y * (i+1) % MOD;
    }
    return x * mypow(y, MOD - 2) % MOD;
}

#define INF 1e8

int n;
VVI g;

int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> n;
    g = VVI(n, VI(n, 0));
    REP(i, n) {
        int u, k;
        cin >> u >> k;
        u--;
        REP(j, k) {
            int v;
            cin >> v;
            v--;
            g[u][v]++;
        }
    }
    VI d(n, -1);
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while(!q.empty()) {
        int f = q.front();
        q.pop();
        REP(i, n) {
            if(g[f][i] && d[i] == -1) {
                d[i] = d[f] + 1;
                q.push(i);
            }
        }
    }
    // vprint(d);
    REP(i, n) {
        cout << i+1 << " " << d[i] << endl;
    }
}

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

class Vertex {
public:
    int num;
    bool flag;                  // 以前訪れたかどうか
    vector<int> adj;            // 隣接リスト
};

int dist[101];
void bfs(map<int, Vertex>& graph, Vertex s) {
    queue<Vertex> que;

    dist[s.num] = 0;
    que.push(s);
    while (!que.empty()) {
        Vertex u = que.front(); que.pop();
        for (auto i = 0; i < u.adj.size(); i++) {
            Vertex v = graph[u.adj[i]];
            if (!v.flag) {
                graph[u.adj[i]].flag = true;
                dist[v.num] = dist[u.num]+1;
                que.push(v);
            }
        }
    }
}

int main(void) {
    int n;
    cin >> n;
    map<int, Vertex> graph; // 頂点の番号->頂点
    for (int i = 0; i < n; i++) {
        Vertex v;
        int num, k;
        cin >> num >> k;
        v.flag = false;
        v.num = num;
        for (int j = 0; j < k; j++) {
            int tmp;
            cin >> tmp;
            v.adj.push_back(tmp);
        }
        graph[num] = v;
    }

    bfs(graph, graph[1]);
    for (int i = 1; i <= n; i++) {
        cout << i << " " << dist[i] << endl;
    }

    return 0;
}