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<algorithm>
using namespace std;

int head, tail;
int Q[100000];
int n, d[110];
bool a[110][110] = {};

void enqueue(int x){
  
  Q[tail] = x;

  if(tail + 1 == 100000) tail = 0;
  else tail++;
}

int dequeue(){
  
  int x = Q[head];

  if(head + 1 == 100000) head = 0;
  else head++;

  return x;
}

void bfs(int u){
  
  // queue<int> Q;
  
  enqueue(u);
  d[u] = 0;
  
  while(head != tail){
    int p = Q[head];
    dequeue();
    
    for(int i = 2;i <= n;i++){
      if(a[p][i] && d[i] == 0){
        enqueue(i);
        d[i] = d[p] + 1;
      }
    }
  }
}

int main(){
  
  int u, k, v;
  
  cin >> n;
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> v;
      a[u][v] = true;
    }
  }
  
  bfs(1);
  
  for(int i = 1;i <= n;i++){
    if(d[i] || i == 1) cout << i << " " << d[i] << endl;
    else cout << i << " " << -1 << endl;
  }
  
  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 <vector>
#include <queue>
#include <algorithm>
using namespace std;

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

int main()
{
    int n,u,k,v;
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> u >> k;
        for(int j = 0; j < k; ++j){
            cin >> v;
            G[u].push_back(v);
        }
    }
    
    fill(d,d + n + 1, -1);
    
    queue<int> que;
    que.push(1);
    d[1] = 0;
    while(!que.empty()){
        int p = que.front(); que.pop();
        for(vector<int>::iterator it = G[p].begin(); it != G[p].end(); ++it){
            if(d[*it] == -1){
                d[*it] = d[p] + 1;
                que.push(*it);
            }
        }
    }
    
    for(int i = 1; i <= n; ++i){
        cout << i << ' ' << d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int adj_matrix[N_MAX+1][N_MAX+1];
array<int,N_MAX+1> depth;

int n;

void bfs(queue<int>& q, const int d) {
    if (q.empty()) { return; }
    queue<int> next_q;
    while (!q.empty()) {
        int node = q.front();
        if (depth[node] < 0) {
            depth[node] = d;
            FOR(i,1,n+1) {
                if (adj_matrix[node][i] && depth[i] < 0) {
                    next_q.push(i);
                }
            }
        }
        q.pop();
    }
    bfs(next_q, d+1);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;

    int id, k, v;
    FOR(i,0,n) {
        cin >> id;
        cin >> k;
        FOR(j,0,k) {
            cin >> v;
            adj_matrix[id][v] = 1;
        }
    }

    depth.fill(-1);

    queue<int> q;
    q.push(1);
    bfs(q, 0);

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

    return 0;
}

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

using namespace std;

int minDistance(vector<vector<int>>& edge, int st, int en) {
	size_t numNode = edge.size();
	vector<bool> passed(numNode, false);
	queue<int> v;
	queue<int> d;

	v.push(st);
	d.push(0);
	passed[st] = true;

	while(!v.empty()) {

		int nowV = v.front(); v.pop();
		int nowD = d.front(); d.pop();

		if(nowV == en) {
			return nowD;
		}

		for(auto nextV : edge[nowV]) {
			if(passed[nextV]) {
				continue;
			}

			v.push(nextV);
			d.push(nowD + 1);
			passed[nextV] = true;
		}

	}

	return -1;
}

int main() {

	int n;
	cin >> n;

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

	for(int i = 0; i < n; i++) {
		cout << i + 1 << " " << minDistance(edge, 0, i) << endl;
	}

	return 0;
}

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

using namespace std;

static const int MAX=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int INF=-1;
int n,adj[MAX+1][MAX+1];
int color[MAX+1],d[MAX+1];
queue<int> Q;

void bfs(){
  for(int i=1;i<=n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }
  d[1]=0;
  color[1]=GRAY;
  Q.push(1);
  while(!Q.empty()){
    int u=Q.front(); Q.pop();
    for(int i=1;i<=n;i++){
      if(adj[u][i] && color[i]==WHITE){
        color[i]=GRAY;
        d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BLACK;
  }
}

int main(){
  cin >> n;
  for(int i=1;i<=n;i++){
    int u,k; cin >> u >> k;
    for(int i=1;i<=n;i++) adj[u][i]=0;
    for(int i=0;i<k;i++){
      int v; cin >> v;
      adj[u][v]=1;
    }
  }
  bfs();
  for(int id=1;id<=n;id++)
    printf("%d %d\n",id,d[id]);
}