Go: package main

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

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

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

int main() {
    // 頂点数と辺数
    int N ; cin >>N;

    // グラフ入力受取
    Graph G(N);
    for (int i = 0; i < N ;i++) {
        int a, b; cin >> a >> b;
        for(int j=0;j<b;j++) {
          int c; cin >>c;
          G[a-1].push_back(c-1);
        }
    }
     // BFS のためのデータ構造
    vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[0] = 0;
    que.push(0); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v]) {
            if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    // 結果出力 (各頂点の頂点 0 からの距離を見る)
    for (int v = 0; v < N; ++v) cout << v+1 << " " << dist[v] << endl;
}

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

using namespace std;

int N;
int D[100+10] = {};
int U = 0;
int K[100+10] = {0};
int V = -1;
int G[100+10][100+10] = {0};



void bfs(int src){
  //cerr << "bfs root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()){
    int cur = Q.front();
    Q.pop();
    //cerr << "visiting " << cur << " " << D[cur] << endl;
    for(int dst=1;dst<=N;++dst){
      if(D[dst] == -1 && G[cur][dst] == 1){
	D[dst] = D[cur]+1;
	Q.push(dst);
      }
    }
  }
}

int main(){
  cin >> N;
  for(int i=0;i<101;++i){
    D[i] = -1;//?????????
  }

  for(int i=0;i<N;++i){
    cin >> U;
    cin >> K[U];
    for(int j=0;j<K[U];++j){
      cin >> V;
      G[U][V] = 1;
    }
  }
  /*
  for(int j=1;j<=N;++j){
    for(int i=1;i<=N;++i){
      cout << G[i][j] << " ";
    }
    cout << endl;
   }
*/

  bfs(1);
  
  for(int i=1;i<=N;++i){
    cout << i << " " << D[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];

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<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
int n;
int a[105][105];
int d[105];
void bfs(int x){
  d[x]=0;
  queue<P > q;
  int y=x;
  for(int i=1;i<=n;i++){
    if(a[y][i])q.push(P(i,d[y]+1));
  }
  while(!q.empty()){
    P p=q.front();q.pop();
    y=p.first;
    if(d[y]!=-1)continue;
    d[y]=p.second;
    for(int i=1;i<=n;i++){
      if(a[y][i])q.push(P(i,d[y]+1));
    }
  }
}
int main(){
  for(int i=0;i<105;i++){
    d[i]=-1;
  }
  cin >> n;
  for(int i=0;i<n;i++){
    int u,m;
    cin >> u >> m;
    for(int j=0;j<m;j++){
      int v;
      cin >> v;
      a[u][v]=1;
    }
  }
  bfs(1);
  for(int i=1;i<=n;i++){
    cout << i<< " " <<d[i] << endl;
  }
  return 0;
}

C++: //＃いんｃぅで＜ｓｔぢお。ｈ＞
#include<stdio.h>
#include<iostream>
using namespace std;
int n=0,u=0;
int k[101],v[101][101],d[101];
int lptr=0,fptr=0;//lptr==enqueue,fptr==dequeue...
int t1[10000];
int s1[10000];
int c=0;//current
int nx=sizeof(t1)/sizeof(t1[0]);
void Enqueue(int s,int t){
	t1[lptr]=t;
	s1[lptr]=s;
	lptr=(lptr+1)%nx;
}
void bfs(int l){
	while(fptr!=lptr){
		l=t1[fptr];
		c=s1[fptr];
		fptr=(fptr+1)%nx;
		if(d[l]!=-1){
			continue;
		}
		for(int i=0;i<k[l];i++){
			if(v[l][i]){
				Enqueue(c+1,v[l][i]);
			}
		}
		d[l]=++c;
	}
}
int main(void){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>u;
		cin>>k[u];
		for(int j=0;j<k[u];j++){
			cin>>v[u][j];
		}
	}
	Enqueue(-1,1);
	for(int i=0;i<n+1;i++){
		d[i]=-1;
	}
	bfs(0);
	
	for(int i=1;i<n+1;i++){
		cout<<i<<" "<<d[i]<<endl;
	}
	
	return 0;
}

C++: /**
 * Bredth first search.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N];

void bfs(int n, int dd) {
	int k = adj[n][0];
	d[n] = dd++;
	for (int j=1;j<=k;j++) {
		bfs(adj[n][j], dd);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
		d[i]=-1;
	}

	bfs(1, 0);

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

	return 0;
}

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

#include <cassert>
#include <climits>

#include <string.h>

#include <vector>
#include <map>
#include <stack>
#include <deque>

enum Color
{
    kWhite,
    kGray,
    kBlack,
};

int n;
int start_vertex = 0;
std::vector< std::vector<int> > matrix;
std::vector<Color> c;
std::vector<int> d;

void breadthFirstSearch()
{
    for (int i = 0; i < n;++i)
    {
        c.push_back(kWhite);
        d.push_back(-1);
    }

    std::deque<int> tmp_queue;
    tmp_queue.push_back(start_vertex);
    c[start_vertex] = kGray;
    d[start_vertex] = 0;

    while (! tmp_queue.empty())
    {
        int current = tmp_queue.front();
        tmp_queue.pop_front();
        c[start_vertex] = kBlack;

        std::vector<int> line = matrix[current];
        for (int i = 0; i < line.size(); ++i)
        {
            if (line[i] > 0 && c[i] == kWhite)
            {
                tmp_queue.push_back(i);
                c[i] = kGray;
                d[i] = d[current] + 1;
            }
        }
    }
}

int main()
{
    std::cin >> n;
    for (int i = 0; i < n; ++i)
    {
        std::vector<int> line;
        for (int j = 0; j < n; ++j)
        {
            line.push_back(0);
        }
        matrix.push_back(line);
    }

    for (int i = 0; i < n; ++i)
    {
        int u;
        int k;

        std::cin >> u;
        std::cin >> k;

        for (int j = 0; j < k; ++j)
        {
            int tmp;
            std::cin >> tmp;
            matrix[u - 1][tmp - 1] = 1;
        }
    }

    breadthFirstSearch();

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

    return 0;
}