Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

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

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include<iostream>
#include<queue>
#include <limits>
using namespace std;
#define N 100

int inf = numeric_limits<int>::max();
int n, M[N][N], d[N];

void bfs(int s) {
    queue<int> q;
    q.push(s);
    for (int i; i<n; i++) {
        d[i] = inf;
    }
    d[s] = 0;
    int u;
    while (!q.empty()) {
        u = q.front(); // Get the first element
        q.pop();
        for (int v=0; v<n; v++) {
            if (M[u][v]==0) {
                continue;  
            }
            if (d[v]!=inf) {
                // Already visited
                continue;
            }
            d[v] = d[u] +1;
            q.push(v);
        }
    }
    for (int i=0; i<n; i++) {
        if (d[i]!=inf) {
            cout << i+1 << " " << d[i] << endl;
        } else {
            cout << i+1 << " " << -1 << endl;
        }
    }
}

int main() {
    int u, k, tmp;
    cin >> n;
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            M[i][j] = 0;
        }
    }
    // Directed graph
    for (int i=0; i<n; i++) {
        cin >> u >> k;
        for (int j=0; j<k; j++) {
            cin >> tmp;
            M[u-1][tmp-1] = 1;
        }
    }

    bfs(0);
    return 0;
}

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

#define INFTY (1 << 30)

void BFS(bool mT[][100], int firstVerVal, int verNum, int distance[]) {
	queue<int>  verQ;
	verQ.push(firstVerVal);
	for (int i = 0; i < verNum; i++)
		distance[i] = INFTY;
	distance[firstVerVal] = 0;

	while (!verQ.empty()) {
		int curVerVal = verQ.front();
		verQ.pop();
		for (int i = 0; i < verNum; i++) {
			if (!mT[curVerVal][i] || distance[i] != INFTY) continue;
			distance[i] = distance[curVerVal] + 1;
			verQ.push(i);
		}
	}
}

int main() {
	bool matrixTable[100][100];
	int distance[100];
	int verNum;
	cin >> verNum;

	for (int i = 0; i < verNum; i++)
		for (int j = 0; j < verNum; j++)
			matrixTable[i][j] = false;
		
	for (int i = 0; i < verNum; i++) {
		int anyVerVal, anyVerNum;
		cin >> anyVerVal >> anyVerNum;
		for (int j = 0; j < anyVerNum; j++) {
			int verValue; cin >> verValue;
			matrixTable[anyVerVal - 1][verValue - 1] = true;
		}
	}

	BFS(matrixTable, 0, verNum, distance);

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

	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int m[105][105],n,v[105],d[105];
void bfs(int s)
{
    v[s]=true;
    d[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=0;i<n;i++)
        {
             if(m[u][i]&&!v[i])
            {
                d[i]=d[u]+1;
                v[i]=true;
                q.push(i);
            }
        }
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)d[i]=-1;
    for(int i=0;i<n;i++)
    {
        int u,v,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            cin>>v;
            m[u-1][v-1]=true;
        }
    }
    bfs(0);
    for(int i=0;i<n;i++)cout<<i+1<<' '<<d[i]<<endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

vector<vector<int>> G;

// O(HW)
int n;
int d[110];
void bfs(){
    queue<int> que;
    
    que.push(0); // (0,0)スタート
    d[0]=0;
    while(!que.empty()){
        int p=que.front();
        que.pop();
        for(int nv : G[p]){
            if(d[nv]!=-1) continue;
            que.push(nv);
            d[nv]=d[p]+1;
        }
    }
}

int main() {
    
    cin>>n;
    G.resize(n);
    rep(i,n){
        int u,k;
        cin>>u>>k;
        u--;
        rep(j,k){
            int v;
            cin>>v;
            v--;
            G[u].push_back(v);
        }
    }

    rep(i,n) d[i]=-1;
    bfs();

    rep(i,n){
        cout<<i+1<<" "<<d[i]<<endl;
    }

}

C++: #include<bits/stdc++.h>
using namespace std;
int i,j,n,u,k,v,g[101][101],d[101],f[101];
int main(void){
  for(i=1;i<101;i++)d[i]=1<<30;
  d[1]=0;
  cin>>n;
  for(i=0;i<n;i++){
    cin>>u>>k;
    for(j=0;j<k;j++){
      cin>>v;
      g[u][v]++;
    }
  }
  queue<int>q;
  q.push(1);
  while(!q.empty()){
    u=q.front();q.pop();
    f[u]=1;
    for(i=1;i<101;i++){
      if(g[u][i]&&!f[i]){
        d[i]=min(d[i],d[u]+1);
        q.push(i);
      }
    }
  }
  for(i=1;i<=n;i++)cout<<i<<" "<<(d[i]==1<<30?-1:d[i])<<endl;
  return 0;
}

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

using namespace std;
const int N = 100;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n;
vector<vector<int>> G(N,vector<int>(N,0));
vector<int> d(N,-1);
vector<int> colors(N,WHITE);

void bfs(){
    queue<int> q;
    q.push(0);
    colors[0] = GRAY;
    d[0] = 0;
    while(!q.empty()){
        int u = q.front();q.pop();
        for(int i = 0;i<n;i++){
            if(colors[i]==WHITE && G[u][i]){
                q.push(i);
                colors[i] = GRAY;
                d[i] = d[u] + 1;
            }
        }
        colors[u] = BLACK;
    }
}

int main()
{
    //freopen("data.txt","r",stdin);
    cin >> n;
    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--;
            G[u][v] = 1;
        }
    }
    bfs();
    for(int i =0;i<n;i++){
        printf("%d %d\n",i+1,d[i]);
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define N 105 // number of nodes
vector<int> neighbors[N]; //start from 1

//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
void bfs(int start, int key,int d){
	if(start == key){
		//if(d < dist[key])
			dist[key] = d;
		return;
	} 
	for(int i = 0; i< neighbors[start].size(); i++){
		bfs(neighbors[start].at(i), key,d+1);
	}
}

int main()
{
    int n;
    scanf("%d",&n);
    memset(dist, -1, sizeof(dist));
    for(int i = 0; i<n; i++){
        int node;
        scanf("%d",&node);
        int num;
        scanf("%d",&num);
        for(int j = 0; j<num; j++){
        	int temp;
        	scanf("%d",&temp);
        	neighbors[node].push_back(temp);
		}
    }
    for(int i = 1; i<=n; i++){
    	bfs(1,i,0);
	}
    for(int i = 1; i<=n; i++){
    	printf("%d %d\n",i, dist[i]);
	}
    return 0;
}