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>
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++ ){
        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;
#define MAX 105

vector<vector<int> > vec;
vector<int> ve;
int dis[MAX];
queue<int> Q;
int n;

int search(int start, int end) {
	while(!Q.empty()) Q.pop();
	Q.push(start);
	for (int i = 1; i <= n; i++) {
		dis[i] = 0;
	}
	int now, len = 0;
	while (!Q.empty()) {
		now = Q.front();
		Q.pop();
		if (now == end)
			return dis[now];
		if (dis[now] == len)
			len++;
		for (int cnt = 1; cnt <= vec[now][0]; cnt++) {
			if (dis[vec[now][cnt]] == 0) {
				dis[vec[now][cnt]] = len;
				Q.push(vec[now][cnt]);
			}
		}
	}
	return -1;
}

int main(void) {
	vec.push_back(ve);
	int u, k, v, i;
	cin >> n;
	for (i = 1; i <= n; i++) {
		ve.clear();
		cin >> u >> k;
		ve.push_back(k);
		while (k--) {
			cin >> v;
			ve.push_back(v);
		}
		vec.push_back(ve);
	}

	for (i = 1; i <= n; i++) {
		cout << i << " " << search(1, 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], 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 v, k ,u;
	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;
		for(int j = 0; j < k; j++) {
			cin >> v;
			M[u-1][v-1] = 1;
		}
	}
	
	bfs(0);
	
	return 0;
	
}

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

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(q->size() > 0)
    {
        int u = q->front();
        q->pop();

        for (int i = 0; i < N; i++)
        {
            if (M[u][i] && C[i] == WHITE)
            {
                C[u] = GRAY;
                D[i] = D[u] + 1;
                q->push(i);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

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

    for (int i = 0; i < N; i++)
    {
        int u, k;
        cin >> u >> k;
        u--;

        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }

    bfs(0);

    for (int i = 0; i < N; i++)
    {
        cout << D[i] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef vector<int> Adj_list;
typedef vector<Adj_list> Adj_lists;

void bfs(const int& i, const Adj_lists& adj_lists, vector<int>& distances){

  distances.at(i-1) = 0;

  queue<int> q;
  q.push(i);

  while(!q.empty()){
    // visit top of queue
    const int& visit = q.front();
    const Adj_list& adj_list = adj_lists.at(visit-1);

    // push adjecent node which is not visited yet
    for(const int& adj : adj_list){
      if(distances.at(adj-1) == -1){
        distances.at(adj-1) = distances.at(visit-1) + 1;
        q.push(adj);
      }
    }

    q.pop();
  }
}

int main(){

  int n;
  scanf("%d", &n);

  Adj_lists adj_lists(n);
  for(int i=0; i<n; i++){
    int u, k;
    scanf("%d %d", &u, &k);
    Adj_list adj_list(k);
    for(int j=0; j<k; j++){
      int v;
      scanf("%d", &v);
      adj_list[j] = v;
    }
    sort(adj_list.begin(), adj_list.end());
    adj_lists[u-1] = adj_list;
  }

  vector<int> distances(n);
  for(int& distance : distances) distance = -1;

  bfs(1, adj_lists, distances);

  for(int i = 0; i < distances.size(); i++){
    printf("%d %d\n", i+1, distances.at(i));
  }

  return 0;
}