Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

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

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

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

using namespace std;

static const int N=100;
static const int INF=-1;
int M[N][N],n;
int d[N];

void bfs(int s){
  queue<int> q;
  q.push(s);

  for(int i=0;i<n;i++){
    d[i]=INF;
  }

  d[s]=0;

  while(!q.empty()){
    int u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==0) continue;
      if(d[v]!=INF) continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }
}

int main(){
  cin>>n;

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

  int u,k,v;

  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);

  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<((d[i]==INF)?(-1):d[i])<<endl;
    }
  return 0;
}

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

int n = 0;
int u = 0;
int k[101];
int v[101][101];
int Short[101] = { 0 };
//into n = 0;
int p = 0;
int fptr = 0;
int lptr = 0;
int x1[10000];
int y1[10000];
void Enqueue(int y, int x)
{
	x1[lptr] = x;
	y1[lptr] = y;
	lptr = (lptr + 1) % n;
}
void bfs(int l) {
	int i;
	while (fptr!=lptr) {
		l=x1[fptr];
		p=y1[fptr];
		fptr = (fptr + 1) % n;
		if(Short[l]!=-1){
			continue;
		}
		for(i=0;i<k[l];i++){
			if(v[l][i]){
				Enqueue(p+1,v[l][i]);
			}
		}
		//Enqueue(v[l][in]);
		Short[l]=++p;
	}
}
int main()
{
	int i;
	int j;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> u;
		cin >> k[u];
		for (j = 0; j < k[u]; j++) {
			cin >> v[u][j];
		}
	}
	Enqueue(-1,1);
	for(i=0;i<n+1;i++){
		Short[i]=-1;
	}
	bfs(0);
	cout << "\n";
	
	for (i = 1; i < n + 1; i++) {
		cout << i << " " << Short[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++){
        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 <iostream>
#include <vector>
#include <queue>
using namespace  std;

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nDmy;

  StDist(int nDist) : m_nDist(nDist), m_nDmy(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();

    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
    {
      if (!cnrvvnAdjcy[nx][ny])          continue;
      if (rvoDist[ny].m_nDist != nIMPOS) continue; 

      int nDist = rvoDist[nx].m_nDist + 1;
      rvoDist[ny].m_nDist = nDist;
      rqnBFSrch.push(ny);
    }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}

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

static const int inf=(1<<21);

int n,m[107][107];
int d[107];

void bfs(int s)
{
	queue<int>q;
	q.push(s);
	for(int i=0;i<n;i++)d[i]=inf;
	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]!=inf)continue;
			d[v]=d[u]+1;
			q.push(v);
		}
	}
	for(int i=0;i<n;i++)
	{
		cout<<i+1<<" "<<((d[i]==inf)?(-1):d[i])<<endl;
	}	
		
}
int main(int argc, char *argv[]) {
	int u,k,v;
	
	cin>>n;
	memset(m, 0, sizeof(m));
	
	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<bits/stdc++.h>

using namespace std;

typedef struct node_{
    int id;
    int d;
    int k;//出次数
    int visited;
    vector<int> v;
}* node;

int main(){
    int n;
    scanf("%d", &n);
    vector<node> graph;
    node dummy;
    graph.push_back(dummy);
    for(int i = 1; i <= n; i++){
        node nd = new node_;
        nd -> d = -1;
        nd -> visited = 0;
        int id, k;
        scanf("%d %d", &id, &k);
        nd -> id = id;
        nd -> k = k;
        for(int i = 0; i < k; i++){
            int v1;
            scanf("%d", &v1);
            nd -> v.push_back(v1);
        }
        graph.push_back(nd);
    }
    graph[1] -> d = 0;

    queue<int> q;
    q.push(1);
    while(q.empty() != 1){
        int num = q.front();
        q.pop();
        node nd = graph[num];
        for(int i = 0; i < nd -> k; i++){
            if(graph[nd -> v[i]] -> d < 0){
                graph[nd -> v[i]] -> d = nd -> d + 1;
                q.push(nd -> v[i]);
            }
        }
    }
    for(int i = 1; i <= n; i++){
        node nd = graph[i];
        printf("%d %d\n", nd -> id, nd -> d);
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int N;
vector<int> dist;
vector<vector<int>> G;
int main(){
    cin >> N;
    dist.assign(N,-1);
    G.resize(N);
    for(int i=0;i<N;i++){
        int id;cin >> id;
        int k;cin >> k;
        for(int j=0;j<k;j++){
            int a;cin >> a;a--;
            G[i].push_back(a);
        }
    }
    queue<int> que;
    que.push(0);
    int d = 0;
    dist[0] = 0;
    while(!que.empty()){
        int now = que.front();
        que.pop();
        for(auto nv:G[now]){
            if(dist[nv]!=-1) continue;
            dist[nv] = dist[now]+1;
            que.push(nv);
        }
    }
    for(int i=0;i<N;i++){
        cout << i+1 << " " << dist[i] << endl;
    }
}