Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

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

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=jp

#include <iostream>
#include <queue>
using namespace std;
static const int INFTY = (1<<21);

int N;
int E[100][100];
int d[100];

void bfs(int s){
  queue<int> q;
  q.push(s);
  for (int j = 0; j < N; j++)d[j]=INFTY;
  d[s] = 0;
  int u;
  while(!q.empty()){
    u = q.front();
    q.pop();
    for(int v = 0; v < N; v++){
      if(E[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for(int k = 0; k < N; k++){
    if(d[k] == INFTY){
      cout << k+1 << " " << -1 << endl;
    }else{
      cout << k+1 << " " << d[k] << endl;
    }
  }
}



int main(){
  //Eへのグラフ情報の代入
  cin >> N;
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      E[i][j] = 0;
    }
  }
  for ( int i = 0; i < N; i++){
    int dummy;
    cin >> dummy;
    int n;
    cin >> n;
    for (int c = 0; c < n; c++){
      int dest;
      cin >> dest;
      E[dummy-1][dest-1] = 1;
    }
  }

  bfs(0);

  return 0;


}

C++: #include <iostream>
#include <queue>
const int MAX_N = 100;
const int INF = 999999999;
using namespace std;

int n;
int color[MAX_N];
bool adj[MAX_N][MAX_N];
int d[MAX_N];

void bfs(int s) {
    queue<int> que; que.push(s);
    for (int i = 0; i < n; i++) d[i] = INF;
    d[s] = 0;

    int u;
    while (!que.empty()) {
        u = que.front(); que.pop();
        for (int v = 0; v < n; v++) {
            if (adj[u][v] && d[v] == INF) {
                d[v] = d[u] + 1;
                que.push(v);
            }
        }
    }
}
int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            adj[i][j] = false;
        }
    }

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

    bfs(0);

    for (int i = 0; i < n; i++) {
        if (d[i] != INF) {
            cout << i+1 << " " << d[i] << endl;
        } else {
            cout << i+1 << " " << -1 << endl;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <stack>
#include <climits>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;

vector<int> G[105];
int d[105];

int main()
{
    int n; cin >> n;
    for(int i = 0; i< n; i++){
        int u,k;
        cin >> u >> k;
        u--;
        for(int j = 0; j < k; j++){
            int x; cin >> x;
            x--;
            G[u].push_back(x);
        }
    }
    for(int i = 0; i < 105; i++){
        d[i] = -1;
    }
    queue<int> q;
    d[0] = 0;
    q.push(0);
    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int i = 0; i < G[u].size(); i++){
            int nv = G[u][i];
            if(d[nv] == -1){
                d[nv] = d[u] + 1;
                q.push(nv);
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout << i+1 << ' ' << d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX = 101;
int n,m[MAX][MAX],d[MAX];
queue<int> s;

void bfs(int num)
{
	for(int i=1;i<=n;i++) d[i]=INT_MAX;
	s.push(num);
	d[num]=0;
	while(!s.empty())
	{
		int u=s.front();
		s.pop();
		for(int i=1;i<=n;i++)
		{
			if(m[u][i]==0) continue;
			if(d[i]!=INT_MAX) continue;
			s.push(i);
			d[i]=d[u]+1;
		}
	}
	for(int i=1;i<=n;i++) cout<<i<<" "<<( d[i]==INT_MAX ? (-1): d[i] )<<endl;
}
int main()
{
	int u,k,t;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>u>>k;
		for(int j=0;j<k;j++)
		{
			cin>>t;
			m[u][t]=1;
		}
	}
	bfs(1);
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------

int n;
bool g[110][110];
int d[110];

void bfs(int s)
{
	d[s] = 0;
	queue<int> q;
	q.push(s);

	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = 1; i <= n; i++) {
			if (g[u][i] && d[i] == -1) {
				q.push(i);
				d[i] = d[u] + 1;
			}
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			g[u][v] = true;
		}
	}

	for (int i = 1; i <= n; i++) d[i] = -1;

	bfs(1);

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

	return 0;
}

C++: #include<stdio.h>
#include<queue>
using namespace std;
int tu[100][100];
int a[100];
int d[100];
int n;
void bfs(int x){
    queue<int> q;
    q.push(x);
    for(int i=0;i<n;i++)d[i]=10000000;
    d[x]=0;
    int u;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int i=0;i<n;i++){
            if(tu[u][i]==0)continue;
            if(d[i]!=10000000)continue;
            d[i]=d[u]+1;
            q.push(i);
        }
        }
        for(int i=0;i<n;i++){
            printf("%d ",i+1);
            if(d[i]==10000000)printf("-1");
            else printf("%d",d[i]);
            printf("\n");
        }
}
int main(){
    scanf("%d",&n);
    int v,m,k;
    for(int i=0;i<n;i++){
        scanf("%d%d",&v,&m);
        v--;
        for(int j=0;j<m;j++){
            scanf("%d",&k);
            k--;
            tu[v][k]=1;
        }
    }
    bfs(0);
    return 0;
}