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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

int main() {
  ll input(n);
  vector<vector<ll>> graph(n);
  REP(i, n) {
    ll input(u, k);
    REP(j, k) {
      ll input(v);
      graph[u - 1].push_back(v - 1);
    }
  }

  vector<bool> visited(n);
  vector<ll> cost(n);
  queue<pair<ll, ll>> que;
  que.push({0, 0});
  while (!que.empty()) {
    pair<ll, ll> node = que.front();
    que.pop();

    if (!visited[node.first]) {
      cost[node.first] = node.second;
    }
    visited[node.first] = true;

    for(auto v: graph[node.first]) {
      if (!visited[v]) {
        que.push({v, node.second + 1});
      }
    }
  }
  REP(i, n) {
    if (!visited[i]) cost[i] = -1;
    cout << i + 1 << " " << cost[i] << endl;;
  }
}

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

using namespace std;

int d[100];
vector<int> adj[100];

void bfs(int n, int start) {
	queue<int> que;

	for (int i = 0; i < n; i++)
		d[i] = (i == start ? 0 : -1);

	que.push(start);
	while(!que.empty()) {
		int from = que.front();
		que.pop();

		for (int i = 0; i < adj[from].size(); i++) {
			int to = adj[from][i];
			if (d[to] == -1) {
				d[to] = d[from] + 1;
				que.push(to);
			}
		}
	}
}

int main() {
	int n;

	cin >> n;

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		while (k--) {
			int v;
			cin >> v;
			adj[u - 1].push_back(v - 1);
		}
	}

	bfs(n, 0);

	for (int i = 0; i < n; i++)
		cout << i + 1 << " " << d[i] << endl;

	return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

#define MAX_N (105)

int n;
bool G[MAX_N][MAX_N];
int dist[MAX_N];
int main()
{
    queue<int> q;
    scanf("%d", &n);
    for ( int i = 1; i <= n; i++ ) {
	int u, k;
	scanf("%d %d", &u, &k);
	for ( int j = 0; j < k; j++ ) {
	    int d;
	    scanf("%d", &d);
	    G[u][d] = true;
	}
    }

    memset(dist, -1, (n+1) * sizeof(int));

    dist[1] = 0;
    q.push(1);
    
    while ( !q.empty() ) {
	int cur = q.front(); q.pop();
	for ( int i = 1; i <= n; i++ ) {
	    if ( G[cur][i] && (dist[i] == -1) ) {
		dist[i] = dist[cur] + 1;
		q.push(i);
	    }
	}
    }

    for ( int i = 1; i <= n; i++ ) {
	printf("%d %d\n", i, dist[i]);
    }

    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
#define NIL -2000000001
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n = 0;
int adl[MAX][MAX], deg[MAX];
int adm[MAX][MAX];
int color[MAX];
int d[MAX];
queue<int> queueBfs;


void bfs() {
  for ( int i = 0; i < n; i++ ) {
    color[i] = WHITE;
    d[i] = -1;
  }
  queueBfs.push(0);
  color[0] = GRAY;
  d[0] = 0;

  while (!queueBfs.empty()) {
    int top = queueBfs.front();

    for (int i = 0; i < n; i++) {
      if (adm[top][i] == 1 && color[i] == WHITE) {
        queueBfs.push(i);
        color[i] = GRAY;
        d[i] = d[top]+1;
      }
    }

    queueBfs.pop();
  }
}

int main() {
  scanf("%d", &n);
  for ( int i = 0; i < n; i++ ) {
    for ( int j = 0; j < n; j++ ) {
      adm[i][j] = 0;
    }
  }
  for ( int i = 0; i < n; i++ ) {
    int num;
    scanf("%d", &num);
    num--;
    scanf("%d", &deg[num]);
    for ( int j = 0; j < deg[num]; j++) {
      scanf("%d", &adl[num][j]);
      adl[num][j]--;
      adm[num][adl[num][j]] = 1;
    }
  }

  bfs();

  for ( int i = 0; i < n; i++ ) {
    printf("%d %d\n", i+1, d[i]);
  }



  return 0;
}

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

const int Max = 105;
int map[Max + 1][Max + 1];

typedef struct{
	bool startflag;
	int distance;
}State;

int main()
{
	int n, k, l, temp;
	std::queue<int> q;
	std::vector<State> state;

	for (int i = 1; i < Max + 1; i++){
		for (int j = 1; j < Max + 1; j++){
			map[i][j] = 0;
		}
	}

	std::cin >> n;
	for (int i = 1; i <= n; i++){
		std::cin >> k >> l;
		for (int j = 1; j <= l; j++){
			std::cin >> temp;
			map[k][temp] = 1;
		}
	}

	State s = { false, 0 };
	for (int i = 0; i <= n; i++){
		state.push_back(s);
	}

	int d = 0;
	while (1){
		if (q.empty() == true){
			bool f = false;
			for (int i = 1; i <= n; i++){
				if (state[i].startflag == false){
					q.push(i);
					state[i].startflag = true;
					d = 0;
					state[i].distance = d;
					d++;
					f = true;
					break;
				}
			}
			if (f == false){
				break;
			}
		}
		int size = q.size();
		for (int j = 0; j < size; j++){
			for (int i = 1; i <= n; i++){
				if (map[q.front()][i] == 1 && state[i].startflag == false){
					q.push(i);
					state[i].startflag = true;
					state[i].distance = d;
				}
			}
			q.pop();
		}
		d++;
	}

	for (int i = 1; i <= n; i++){
		std::cout << i << " " << state[i].distance << std::endl;
	}

	return 0;
}

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

int M[105][105];
int n;
int d[105];
int INFTY =(1<<21);

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

}

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