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 int N = 100;
int n;
int M[N][N], color[N], d[N];

int main(){
  queue<int> q;
  cin >> n;
  int u, k, v;
  for(int i=0;i<n;i++){
    cin >> u >> k;
    for(int j=0;j<k;j++){
      cin >> v;
      M[u-1][v-1] = 1;
    }
  }
  for(int i=0;i<n;i++) d[i] = -1;
  d[0] = 0;
  color[0] = 1;
  q.push(0);
  int x;
  while(q.empty() == false){
    x = q.front();
    q.pop();
    for(int i=0;i<n;i++){
      if(M[x][i] == 0) continue;
      if(color[i] == 0){
        color[i] = 1;
        d[i] = d[x] + 1;
        q.push(i);
      }
    }
  }
  for(int i=0;i<n;i++){
    cout << i+1 << " " << d[i] << endl;
  }
  return 0;
}

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

using namespace std;

int n;
int vertex[101];
int graph[101][101];
int dis[101];

queue<int> Q;

int main() {
	cin >> n;
	int k,d;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			graph[i][j] = -1;
		}
		dis[i] = -1;
	}


	for (int i = 1; i <= n; ++i) {
		cin >> vertex[i] >> k;
		for (int j = 0; j < k; ++j) {
			cin >> d;
			graph[i][d] = 1;
		}
	}

	Q.push(1);
	dis[1] = 0;

	while (!Q.empty()) {
		k = Q.front();
		Q.pop();
		for (int i = 1; i <= n; ++i) {
			if (graph[k][i] == 1 && dis[i] == -1) {
				Q.push(i);
				dis[i] = dis[k]+1;
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		cout << i << ' ' << dis[i] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
vi M(MAX, -1);
int main() {
	int n; cin >> n;
	M[1] = 0;
	deque<pair<int, int>> dq;
	int u, k, v;
	rep(i, 0, n) {
		cin >> u >> k;
		rep(j, 0, k) {
			cin >> v;
			if(u == 1) M[v] = 1;
			else dq.eb(mp(u, v));
		}
	}
	while(!dq.empty()) {
		tie(u, v) = dq.front(); dq.pop_front();
		if(M[u] == -1) dq.eb(mp(u, v));
		else if(M[v] == -1 || M[v] > M[u] + 1) M[v] = M[u] + 1;
	}
	rep(i, 1, n + 1) {
		cout << i << " " << M[i] << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<queue>
#define MAX 100
#define INFTY -1

using namespace std;
queue<int> Q;
int d[MAX];
int A[MAX][MAX]={0};
int n;

void bfs(int u)
{
	Q.push(u);
	d[u] = 0;

	while(!Q.empty())
	{
		int v = Q.front();
		Q.pop();
		for(int i=0; i<n; i++)
		{
			if(A[v][i] == 1)
			{
				if(d[i] == INFTY)
				{
					Q.push(i);
					d[i] = d[v] + 1;
					//printf("%d %d\n",v+1,d[v]);
				}
			}
		}
	}
}

int main()
{
	int u,k,v;
	scanf("%d", &n);
	for(int i=0; i<n; i++)
	{
		scanf("%d%d", &u, &k);
		u -= 1;
		for(int j=0; j<k; j++)
		{
			scanf("%d", &v);
			v -= 1;
			A[u][v] = 1;
		}
		d[u] = -1;
	}
	
	bfs(0);

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

	return 0;
}

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

int n,u,k,v;
int g[102][102];
int d[102];
int sn[102];
int cnt=0;
int x,siz;

queue<int>q;

int main(){
  cin>>n;
  memset(g,0,sizeof(g));
  for(int i=1;i<=n;i++){
    cin>>u>>k;
    d[i]=-1;
    sn[i]=0;
    for(int j=1;j<=k;j++){
      cin>>v;
      g[u][v]=1;
    }
  }
  
  q.push(1);

  while(!q.empty()){

    siz=q.size();
    cout<<siz<<endl;

    for(int i=0;i<siz;i++){
      
      x=q.front();
      q.pop();
      
      sn[x]=1;
      if(d[x]==-1)d[x]=cnt;
      
      for(int j=1;j<=n;j++){
	
	if(g[x][j]==1 && sn[j]==0){
	  q.push(j);
	}
      
      }

    }

    cnt++;

  }

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

  }

}

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

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(!q->empty())
    {
        int u = q->front();
        q->pop();

        for (int v = 0; v < N; v++)
        {
            if (M[u][v] && C[v] == WHITE)
            {
                C[v] = GRAY;
                D[v] = D[u] + 1;
                q->push(v);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

        for (int j = 0; j < N; j++)
        {
            M[i][j] = 0;
        }
    }

    for (int i = 0; i < N; i++)
    {
        int u, k;
        cin >> u >> k;
        u--;

        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

    for (int i = 0; i < N; i++)
    {
        cout << i+1 << " " << D[i] << endl;
    }

    return 0;
}

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


int main() {
	int k[110][110];
int n,l,a,b;
cin>>n;
for(int j=0;j<n;j++){
	cin>>l>>a;
	for(int i=0;i<a;i++){
		cin>>b;
		k[j][b-1]=1;
	}
}
int q[105];
for(int j=0;j<n;j++){
	q[j]=-1;
	if(k[0][j]==1){
		q[j]=1;
	}
}
q[0]=0;
int u=1;
int m=1;
while(m<=n){

	for(int d=0;d<n;d++){
		if(q[d]==m){
			for(int h=0;h<n;h++){
				if(k[d][h]==1 &&q[h]==-1){
					q[h]=m+1;
				}
			}
		}
	}
m=m+1;	
}
for(int y=0;y<n;y++){
	cout<<y+1<<" "<<q[y]<<endl;
}

	return 0;
}