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<bits/stdc++.h>
using namespace std;

#define MAX 100
#define NIL -2

int n, G_list[MAX+1];
int G[MAX+1][MAX+1];
int d[MAX+1][2];

void init(void){
  for(int i=0; i<=MAX; i++){
    d[i][0]=-1;
  }
}

void make_graph(void){
  scanf("%d", &n);

  for(int i=0; i<n; i++){
    int cnt, num, m;

    scanf("%d %d", &num, &cnt);
    G_list[i]=num;

    for(int j=0; j<cnt; j++){
      scanf("%d", &m);
      G[num][m]=1;
    }
  }
}

void check_depth(void){
  queue<int> Q;
  Q.push(G_list[0]);
  d[G_list[0]][0]=0;
  d[G_list[0]][1]=NIL;
  
  while(!Q.empty()){
    int top=Q.front();
    Q.pop();
    
    for(int j=0; j<n; j++){
      if(G[top][G_list[j]] == 1 && d[G_list[j]][1] != NIL){
	d[G_list[j]][0]=d[top][0]+1;
	d[G_list[j]][1]=NIL;
	Q.push(G_list[j]);
      }
    }
  }
}

void print(void){

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

int main(void){

  init();
  make_graph();
  check_depth();
  print();
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
using vll=vector<ll>;
using vi=vector<int>;
using vb=vector<bool>;
using pii=pair<int,int>;

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}

//pow(llpow,modpow)
template<class T> ll llpow(ll x,T n){ll ans=1;if(x==0)ans=0;while(n){if(n&1)ans*=x;x*=x;n>>=1;}return ans;}
long long modpow(long long a, long long n, long long mod) {long long res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}
//最大公約数
template<class T> inline T gcd(T x,T y){if(y==0)return x; else {return gcd(y,x%y);}}
//最小公倍数
template<class T> inline T lcm(T x,T y){return x/gcd(x,y)*y;}
//逆元
long long modinv(long long a, long long m) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)

long long INF = 1LL<<60;
int main( ){
    int n;
    cin>>n;
    vi conne[n];
    rep(i,0,n){
        int id;cin>>id;
        id--;
        int r; cin>>r;
        rep(j,0,r){
            int in; cin>>in; in--;
            conne[id].push_back(in);
        }
    }
    vi dist(n,-1);
    queue<pii> q;
    q.emplace(0,0);
    while(!q.empty()){
        pii n=q.front(); q.pop();
        int i=n.first; int d=n.second;
        if(dist[i]==-1)dist[i]=d;
        for(int itr:conne[i]){
            if(dist[itr]!=-1)continue;
            q.emplace(itr,d+1);
        }
    }
    rep(i,0,n)cout<<i+1<<" "<<dist[i]<<endl;
    return 0;
}

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

int n,d[100];
bool graph[100][100], visited[100];
queue<int> q;


void bfs(int s){
    q.push(s);
    visited[0]=true;

    while(!q.empty()){
        int v=q.front(); q.pop();
        for(int v2=0; v2<n; v2++){
            if(graph[v][v2] && visited[v2]==false){
                d[v2]=d[v]+1; visited[v2]=true;
                q.push(v2);
            }
        }
    }
}


int main(void){
    cin >> n;

    for(int i=0; i<n; i++){
        visited[i]=false;
        d[i]=-1;
        for(int j=0; j<n; j++) graph[i][j]=false;
    }
    for(int i=0; i<n; i++){
        int v,k; cin >> v >> k;
        for(int j=0; j<k; j++){
            int w; cin >> w;
            graph[v-1][w-1]=true;
        }
    }
    d[0]=0;


    q.push(0);
    bfs(0);

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

}

C++: from collections import deque
n = int(input())
A = [0]*(n+1)
D = [-1]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
d.append(1)
D[1] = 0
DP[1] = 0
while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i)
for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp)

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
mat g;

lli u,k,v;

queue<lli> q;
vbl vis;
lli p;
vll dp;

lli solve(lli x){
    if(x == 1) return dp[x] = 0;
    if(dp[x]) return dp[x];

    lli ret = dp[x] = -1;
    for(lli i = 0;i < g[x].size();i++){
        ret = 
        solve(g[x][i]) < 0 
        ? ret 
        : ret < 0 
        ? solve(g[x][i]) + 1 
        : min(ret,solve(g[x][i])+1);
    }
    return dp[x] = ret;
}

int main(){
    cin >> n;
    g = mat(n+1);
    dp = vll(n+1);
    for(lli i = 0;i < n;i++){
        cin >> u >> k;
        for(lli j = 0;j < k;j++){
            cin >> v;
            g[v].push_back(u);
        }
    }
    for(lli i = 1;i <= n;i++){
        cout << i << " " << solve(i) << endl;
    }
    return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<int>* adj = new vector<int>[n];
	int* dist = new int[n];
	int t1, t2 ,t3;
	for (int i = 0;i < n;i++) {
		cin >> t1;
		t1--;
		cin >> t2;
		for (int i = 0;i < t2;i++) {
			cin >> t3;
			adj[t1].push_back(t3 - 1);
		}
		dist[i] = -1;
	}
	queue<pair<int, int> > q;
	q.push(make_pair(0, 0));
	dist[0] = 0;
	while (!q.empty()) {
		pair<int, int> p = q.front();
		q.pop();
		for (int i = 0;i < adj[p.second].size();i++) {
			t1 = adj[p.second][i];
			if (dist[t1] == -1){
				q.push(make_pair(p.first + 1, t1));
				dist[t1] = p.first + 1;
			}
		}
	}
	for (int i = 0;i < n;i++) {
		cout << i + 1 << " " << dist[i] << endl;
	}

}