Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);
int n,M[N][N];
int d[N];
void bfs(int s)
{
  queue<int> q;
  q.push(s);
  for(int i = 0;i<n;i++)
     d[i] = INFTY;
  d[s] = 0;
  int u;
  while (!q.empty())
  {
    u = q.front(),q.pop();
    for(int v = 0;v<n;v++)
    {
      if(M[u][v] == 0) continue ;
      if(d[v]!= INFTY ) continue ;
      d[v] = d[u]+1;
      q.push(v);
    }
  }
  for(int i = 0;i<n;i++)
  {
    cout<<i+1<<" "<<((d[i] == INFTY) ? (-1) : d[i] ) <<endl;
  
  }
}
int main()
{
  int u,k,v;
  cin>>n;
  for(int i = 0;i<n;i++)
  {
    for(int j=  0;j<n;j++)
       M[i][j] = 0;
  }
  for(int i = 0;i<n;i++)
    {
      cin>>u>>k;
      u--;
      for(int j = 0;j<k;j++)
      {
        cin>>v;
        v--;
        M[u][v] = 1;
      }
    }
  bfs(0);
 return 0;
}

C++: #include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;


const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

long long int n,u[105],k[105],v[105][105],used[105]={},num[105]={};
vector<long long int>vec[200];
queue<long long int>que;
int main()
{
    cout << fixed << setprecision(18);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>u[i]>>k[i];
		u[i]--;
		for(int j=0;j<k[i];j++){
			cin>>v[i][j];
			v[i][j]--;
			vec[u[i]].push_back(v[i][j]);
		}
	}
	que.push(0);
	
	for(int i=0;i<n;i++){
		num[i]=-1;
	}
	num[0]=0;
	used[0]=true;
	while(!que.empty()){
		
		used[que.front()]=true;
		for(int i=0;i<vec[que.front()].size();i++){
			if(!used[vec[que.front()][i]]){que.push(vec[que.front()][i]);
			num[vec[que.front()][i]]=num[que.front()]+1;used[vec[que.front()][i]]=true;}			
		}
		que.pop();
	}
	
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<num[i]<<endl;
	}
}

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

using namespace std;

static const int N = 100;
static const int INFTY = (1<<21);

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

void bfs(int s){
  queue<int> q;  //標準ライブラリを使用する(テストでは使用×)
  q.push(s);
  for(int i = 0; i < n; i++) d[i] = INFTY;
  d[s] = 0;
  int u;
  while(!q.empty()){
    u = q.front();
    q.pop();
    for(int v = 0; v < n; v++){
      if(M[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for(int i = 0; i < n; i++){
    cout << i+1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
  }
}

int main(){
  int u,k,v;

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

  return 0;
}

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

int main()
{
	int i,j,n,k,e[101][101],v[101],a,b,d[101],s;
	queue<int> Q;
	
	for(i=0;i<=100;i++)
	{
		for(j=0;j<=100;j++)
		{
			e[i][j]=0;
		}
		v[i]=0;
		d[i]=-1;
	}
	scanf("%d",&n);
	for(i=0;i<=n-1;i++)
	{
		scanf("%d%d",&a,&k);
		for(j=0;j<=k-1;j++)
		{
			scanf("%d",&b);
			e[a][b]=1;
		}
	}
	
	Q.push(1);
	d[1]=0;
	s=1;
	v[1]=1;
	while(Q.empty()!=1)
	{
		i=Q.front();
		for(j=1;j<=n;j++)
		{
			if(e[i][j]==1 && v[j]==0)
			{
				d[j]=d[i]+1;
				v[j]=1;
				Q.push(j);
			}
		}
		Q.pop();
	}
	for(i=1;i<=n;i++)
	{
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
void bfs(int);
int n,M[N][N],d[N],u,i,j;
string color[N];
int main(){
  int inu,k,v;
  cin >> n;
  for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
      M[i][j]=0;
    }
  }
  for(i=1;i<=n;i++){
    cin >> inu >> k;
    for(j=0;j<k;j++){
      cin >> v;
      M[inu][v]=M[v][inu]=1;
    }
  }
  bfs(1);
  for(i=1;i<=n;i++){
    if(d[i]>n) cout << i << ' ' << -1 << endl;
    else cout << i << ' ' << d[i] << endl;
  }
  return 0;
}
void bfs(int s){
  queue<int> q;
  for(i=1;i<=n;i++){
    color[i]="WHITE";
    d[i]=1000000000;
  }
  color[s]="GLAY";
  d[s]=0;
  q.push(s);
  while(q.empty()!=true){
    u=q.front();
    q.pop();
    for(i=1;i<=n;i++){
      if(M[u][i]&&color[i]=="WHITE"){
	color[i]="GLAY";
	d[i]=d[u]+1;
	q.push(i);
      }
    }
    color[u]="BLACK";
  }
}