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++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

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

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

    for(int i=0;i<n;i++){
        dis_min[i]=-1;
    }

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<"\n";
    }
    return 0;
}

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

using namespace std;

static const int MAX = 100;
static const int WHITE = 1;
static const int GRAY = 2;
static const int BLACK = 3;

class BFS {
private:
    int color[MAX];
    int n;
    int M[MAX][MAX];
    queue<int> Q;
    int distance[MAX];

public:
    BFS()
    {
        cin >> n;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                M[i][j] = 0;
        int vertex;
        int degree;
        for(int i = 0; i < n; i++)
        {
            color[i] = WHITE;
            cin >> vertex;
            cin >> degree;
            int to_v;
            for(int j = 0; j < degree; j++)
            {
                cin >> to_v;
                M[vertex - 1][to_v - 1] = 1;
            }
        }
    }

    void bfs()
    {
        for(int i = 0; i < n; i++)
            distance[i] = -1;

        distance[0] = 0;
        Q.push(0);
        color[0] = GRAY;
        while(Q.size() != 0)
        {
            int u = Q.front();
            Q.pop();
            color[u] = BLACK;
            for(int v = 0; v < n; v++)
            {
                if(M[u][v] && color[v] == WHITE)
                {
                    distance[v]  = distance[u] + 1;
                    Q.push(v);
                    color[v] = GRAY;
                }
            }
        }
    }

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

int main()
{
    BFS bfs;
    bfs.bfs();
    bfs.print();
}

C++: #include <iostream>
#include <stdio.h>
#include <queue>
#define N 101
#define INFTY (1<<21)


int n;
int M[N][N];
int d[N];

void bfs(int u) {
    std::queue<int> Q;
    Q.push(u);
    for (int i = 0; i < n; i++) d[i] = INFTY;
    d[u] = 0;
    while ( ! Q.empty() ) {
        int j = Q.front();
        Q.pop();
        for (int v = 0; v < n; v++) {
            if (M[j][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[j] + 1;
            Q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}

int main() {
    int u, k, v;
    scanf("%d", &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++) {
        scanf("%d %d", &u, &k);
        u--;
        if (k == 0) continue;
        for (int j = 0; j < k; j++) {
            scanf("%d", &v);
            v--;
            M[u][v] = 1;
        }
    }
    
    bfs(0);
    
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
 
#define N 100
#define INFTY 1000000
 
int M[N][N],d[N];
int n,u,k,v;
 
//bfs
void bfs(int s){
 
  queue <int> Q;
  //十分大きな数字に初期化
  for(int i = 0; i < N; i++ ) d[i] = INFTY;
  
  //最初の訪問先
  d[s] = 0;
  Q.push(s);

  //Qが空でないとき
  while ( !Q.empty() ){

    //queueから出す
    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;

      //queueに追加
      Q.push(v);
    }
  }
 
  //出力
  for( int i = 0; i < n; i++ ){
    cout << i+1 << " ";
    if ( d[i] == INFTY ) cout << -1;
    else cout << d[i];
    cout << endl;
  }
 
}
 
 
int main(){

  //初期化
  for( int i = 0; i < n; i++ ){
    for( int j = 0; j < n; j++ ) M[u][v] = 0;
  }
  
  //入力
  cin >> n;
  for( int i = 0; i < n; i++ ){
    cin >> u;
    u--;
    cin >> k;
    for( int j = 0; j < k; j++ ){
      cin >> v;
      v--;
      //つながっているとき
      M[u][v] = 1;
    }
  }
  
  //bfs
  bfs(0);
   
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

vector<int> d(101, -1);
vector<vector<int> > c(101, vector<int>(101, 0));

int n, u, k, v;
typedef pair<int, int> P; 
int bfs(void) {
    queue<P> que;
    que.push(P(0, 0));
    while(que.size()>0) {
        P p = que.front(); que.pop();
        if(d[p.first]==-1) d[p.first] = p.second;
        for(int i=0; i<n; ++i) {
            if(c[p.first][i]==1 && d[i]==-1) que.push(P(i, d[p.first]+1));
        }
    }
}

int main() {
    cin >> n;
    for(int i=0; i<n; ++i) {
        cin >> u >> k;
        for(int j=0; j<k; ++j) {
            cin >> v;
            c[i][v-1] = 1;
        }
    }
    bfs();
    for(int i=0; i<n; ++i) {
        cout << i+1 << " " << d[i] << endl;
    }
    return 0;
}