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<vector>
#include<queue>
using namespace std;

void calc(const vector<vector<int> >& G, vector<int>& D,
                                        queue<int>& Q, int dim) {
    if (Q.empty()) return;
    queue<int> Q2;
    while (!Q.empty()) {
        int i = Q.front();
        for (int j = 1; j < G[i].size(); j++) {
            if (G[i][j] && D[j] == -1) {
                Q2.push(j);
                D[j] = dim;
            }
        }
        Q.pop();
    }
    calc(G, D, Q2, dim + 1);
}

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

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << G[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;

    vector<int> D(n + 1, -1);

    queue<int> Q;
    Q.push(1);
    D[1] = 0;
    calc(G, D, Q, 1);

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

    return 0;
}

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

using namespace std;

int n;
int u[102], k[102], t[102][102];
queue<int>Q;
vector<int>V;
int color[102];
int d[102];

void bfs(int s){
  for(int i = 1; i <= n; ++i){
    if(u[i] != s){
      color[u[i]] = 0;
      d[u[i]] = 150;
    }
  }
  d[s] = 0;
  int pi = 0;
  Q.push(s);
  while(!Q.empty()){
    int m = Q.front();
    Q.pop();
    for(int i = 1; i <= k[m]; ++i){
      if(color[t[m][i]] == 0){
        color[t[m][i]] = 2;
        d[t[m][i]] = d[m] + 1;
        pi = t[m][i];
        Q.push(t[m][i]);
      }
    }
    color[m] = 1;
  }
  for(int i = 1; i <= n; ++i){
    if(d[i] == 150){
      cout << i << " " << -1 << endl;
    }else{
      cout << i << " " << d[i] << endl;
    }
  }
  
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; ++i){
    cin >> u[i] >> k[i];
    for(int j = 1; j <= k[i]; ++j){
      cin >> t[i][j];
    }
  }
  bfs(1);
}

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<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 <iostream>
#include <queue>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int adj_matrix[101][101] = {0};
    int visit[101] = {0};
    queue<int> q;
    int dist_from_start[101] = {0};
    for (int i = 1; i <= n; i++)
    {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++)
        {
            int x;
            cin >> x;
            adj_matrix[u][x] = 1;
        }
    }

    q.push(1);
    visit[1] = 1;
    while (!q.empty())
    {
        int now = q.front();
        for (int i = 1; i <= n; i++)
        {
            if (adj_matrix[now][i] == 1 && visit[i] == 0)
            {
                visit[i] = 1;
                q.push(i);
                dist_from_start[i] = dist_from_start[now] + 1;
            }
        }
        q.pop();
    }

    for (int i = 1; i <= n; i++)
    {
        if (i == 1 || dist_from_start[i] > 0)
        {
            cout << i << " " << dist_from_start[i] << endl;
        }
        else
        {
            cout << i << " " << -1 << endl;
        }
    }

    return 0;
}