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 <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> edges(n);
    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--;
            edges[u].push_back(v);
        }
    }

    vector<int> ds(n, -1);
    ds[0] = 0;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto v : edges[u]) {
            if (ds[v] >= 0) {
                continue;
            }
            ds[v] = ds[u] + 1;
            q.push(v);
        }
    }

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

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

int G[101][101];
int dis[101];
int main(){
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    int u,k;
    cin>>u>>k;
    for(int j=1;j<=k;j++){
      G[u][0]=k;
      cin>>G[u][j];
    }
  }
  fill(dis,dis+101,INF);
  dis[1]=0;
  queue<int> Q;
  Q.push(1);
  while(!Q.empty()){
    int num=Q.front(); Q.pop();
    for(int i=1;i<=G[num][0];i++){
      int k=G[num][i];
      dis[k]=min(dis[num]+1,dis[k]);
      Q.push(k);
    }
  }
  for(int i=1;i<=n;i++){
    cout<<i<<" ";
    if(dis[i]==INF)cout<<-1<<"\n";
    else  cout<<dis[i]<<"\n";
  }
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
	int check_id;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
	check_id = 0;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	//if (id == check_id) return -1;
	//else check_id = id;

	if (id == u) {
		//check_id = 0;
		return 0;
	}

	for (int i = 0; i < k; i++) {
		if (id == next[i]->u) {
			//check_id = 0;
			return 1;
		}
	}
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d_min, d + 1);
	}
	//check_id = 0;
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

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

	delete[] adjl;
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<functional>
#include<queue>
#include<stack>
#include<math.h>
#define INF 999999
#define EPS 1.0e-6

using namespace std;

vector<int> G[105];
int n;
int d[105];

void bfs(int s)
{
	fill(d, d + n, -1);
	d[s] = 0;

	queue<int> que;
	que.push(s);
	while (!que.empty()) {
		int k = que.front(); que.pop();
		for (int i = 0; i < G[k].size(); i++) {
			int j = G[k][i];
			if (d[j] == -1) {
				d[j] = d[k] + 1;
				que.push(j);
			}
		}
	}
}

int main()
{
	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].push_back(v);
		}
	}
	bfs(0);
	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << d[i] << endl;
	}

	return 0;
}

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

int main(){
    int n;
    cin >> n;
    vector<vector<bool>> g(n, vector<bool>(n, false));
    int u, k;
    for (int i = 0; i < n; i++){
        cin >> u >> k;
        u--;
        int v;
        for (int j = 0; j < k; j++){
            cin >> v;
            v--;
            g[u][v] = true;
        }
    }
    vector<int> color(n, 0);
    vector<int> min_distance(n, -1);
    queue<int> bfs;
    bfs.push(0);
    min_distance[0] = 0;
    color[0]++;
    while (bfs.size()){
        int u = bfs.front();
        bfs.pop();
        for (int i = 0; i < n; i++){
            if (g[u][i] && color[i] == 0){
                bfs.push(i);
                color[i]++;
                min_distance[i] = min_distance[u] + 1;
            }
        }
        color[u]++;
    }

    for (int i = 0; i < n; i++){
        cout << i + 1 << ' ' << min_distance[i] << endl;
    }
}