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<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAX=105;
int M[MAX][MAX];
int dis[MAX],n;
bool inq[MAX];
void bfs(int cur){
	queue<int> Q;
	Q.push(cur);
	dis[cur]=0;
	while(!Q.empty()){
		int u=Q.front();
		Q.pop();
		for(int i=1;i<=n;i++){
			if(M[u][i]&&!inq[i]){
				if(dis[i]==-1)dis[i]=dis[u]+1;
				else dis[i]=min(dis[i],dis[u]+1);
				Q.push(i);
				inq[i]=1;
			}
		}
	}
}
int main(){
	int u,k;
	cin>>n;
	memset(M,0,sizeof(M));
	memset(dis,-1,sizeof(dis));
	memset(inq,0,sizeof(inq));
	for(int i=0;i<n;i++){
		cin>>u>>k;
		int v;
		for(int j=0;j<k;j++){
			cin>>v;
			M[u][v]=1;
		}
	}
	bfs(1);	
	for(int i=1;i<=n;i++){
		printf("%d %d\n",i,dis[i]);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#define N 100
using namespace std;

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


void bfs(int s) {
	queue<int> Q;
	for (int i = 0;i < n;i++) d[i] = -1;
	d[s] = 0;
	Q.push(s);
	color[s] = 1;
	while (!Q.empty()) {
	  for (int i = 0;i < n;i++)
		  if (M[Q.front()][i] == 1) {
			  if (color[i] == 0) {
				  d[i] = d[Q.front()] + 1;
				  color[i] = 1;
				  Q.push(i);
			  }
		  }
		  Q.pop();
	}
	for (int i = 0;i < n;i++)
		cout << i + 1 << " " << d[i] << endl;
}


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

	bfs(0);

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <map>
#include <cmath>
#include <queue>

using namespace std;

static const int N = 100;

int main() {

    int graph[N][N] = {0};
    int distance[N] = {-200000};
    bool alreadySearched[N] = {false};

    int n;scanf("%d",&n);
    for(int i=0;i<n;i++){
        int u;scanf("%d",&u);
        u--;
        int k;scanf("%d",&k);
        for(int j=0;j<k;j++){
            int v;scanf("%d",&v);
            v--;
            graph[u][v] = 1;
        }
    }


    queue<int> Q;
    Q.push(0);
    distance[0] = 0;
    alreadySearched[0] = true;

    while(!Q.empty()){
        int u = Q.front();
        for(int i=0;i<n;i++){
            if(!alreadySearched[i] && graph[u][i] == 1){
                Q.push(i);
                alreadySearched[i] = true;
                distance[i] = distance[u] + 1;
            }
        }
        Q.pop();
    }

    for(int i=0;i<n;i++){
        if(!alreadySearched[i]){
            distance[i] = -1;
        }
    }
    for(int i=0;i<n;i++){
        printf("%d %d\n", i+1, distance[i]);
    }

    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];  // 距離で訪問状態(color)を管理する

void bfs(int s)
{
    queue<int> q;  // 標準ライブラリのqueueを使用
    q.push(s);
    for (int i = 0; i < n; i++) {
        d[i] = INFTY;  // 距離を十分に大きい値に初期化する
    }
    d[s] = 0;  // s(始点)の距離を0とする
    int u;
    while(!q.empty()) {  // キューにキーがある限り実行
        u = q.front();  // キューの一番上の要素をuにして
        q.pop();  // 削除
        for (int v = 0; v < n; v++) {
            if (M[u][v] == 0) {  // uからvへのパスがなければ次へ
                continue;
            }
            if (d[v] != INFTY) {  // 距離が十分に大きくなければ次へ
                continue;
            }
            d[v] = d[u] + 1;  // 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0; i<(n); i++)
#define REP2(i,x,n) for(ll i=x; i<(n); i++)
#define REP3(i,x,n) for(ll i=x; i>(n); i--)
#define ALL(n) begin(n),end(n)
#define MAX 500000
#define SENTINEL 2000000000
#define NIL -1
static const int N=100;
static const int INFTY = (1<<21);
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
const long long INF = numeric_limits<long long>::max();
int n,M[N][N];
int d[N];//訪問状態の管理


void bfs(int s){
    queue<ll>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;
#define N 100

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] = -1;
    }
    d[s] = 0;
    int u;
    while(!q.empty()) {
        u = q.front();
        q.pop();
        for(int v=0; v<n; v++) {
            if(M[u][v]) {
                continue;
            }
            if(d[v]!=-1) {
                continue;
            }
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for(int i=0; i<n; i++) {
        cout << i+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;
}