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++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

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

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";

    //output
    for(int i=0;i<n;i++){
        std::cout<<i+1<<' '<<dis_min[i]<<std::endl;
    }

    return 0;
}

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

const int inf = 1<<29;
int n;
vector<int> d(110, inf);
vector< vector<bool> > G(110, vector<bool>(110, false));

void bfs(int s){

    queue<int> q;
    d[s] = 0;
    q.push(s);

    while(!q.empty()){
        int now = q.front(); q.pop();
        for(int i=1; i<=n; ++i){
            if(G[now][i] == true && d[now]+1 < d[i]){
                d[i] = d[now] + 1;
                q.push(i);
            }
        }
    }

}

int main(){
    cin >> n;

    for(int i=0; i<n; ++i){
        int id, k; cin >> id >> k;
        for(int j=0; j<k; ++j){
            int v; cin >> v;
            G[id][v] = true;
        }
    }

    bfs(1);

    for(int i=1; i<=n; ++i){
        cout << i << " ";
        if(d[i] == inf) cout << -1;
        else cout << d[i];
        cout << endl;
    }
}

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

#define N 1000
#define UNDEFINED -1

int n, u, k, v;
int G[N][N];
std::queue<int> Q;
int d[N];

int main() {
    for(int i = 0; i < N; i++) {
        d[i] = UNDEFINED;
        for(int j = 0; j < N; j++) {
            G[i][j] = 0;
        }
    }

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

    d[1] = 0;
    Q.push(1);
    while(!Q.empty()) {
        int q = Q.front();
        Q.pop();
        for(int i = 1; i <= n; i++) {
            if(G[q][i] != 0 && d[i] == UNDEFINED){
                d[i] = d[q] + 1;
                Q.push(i);
            }
        }
    }

    for(int i = 1; i <= n; i++) {
        printf("%d %d\n", i, d[i]);
    }

    return 0;
}

C++: /*
Breadth First Search(https://onlinejudge.u-aizu.ac.jp/#/problems/ALDS1_11_C)
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define rep(n, i) Rep(0, n-1, i)
#define pb(v) push_back(v)
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

const int MAX = 128;

//隣接リスト
vector <int> adj[MAX];
//頂点１からの距離
int d[MAX];

void solve(void){
	//init
	ENJYU;
	memset(d, -1, sizeof(d));
	//input
	int N;
	cin >> N;
	rep(N, i) {
		int u, k;
		cin >> u >> k;
		u--;
		rep(k, i) {
			int v;
			cin >> v;
			v--;
			adj[u].pb(v);
		}
	}
	//bfs
	queue <int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int n = q.front();
		q.pop();
		for (int nv : adj[n]) {
			if (d[nv] >= 0) {
				continue;
			}
			d[nv] = d[n] + 1;
			q.push(nv);
		}
	}

	rep(N, i) cout << i+1 << ' ' << d[i] << endl;

}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}

C++: #include<algorithm>
#include<bitset>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
#include<complex>
#include<list>
#include<cstdio>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)


using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;




const int INF = 1e9 + 7;


/******************************************************************************************/



vector<int> G[105];
int dist[105];


int main() {

	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(10);

	int n;
	cin >> n;

	for (int i = 0; i < 105; i++)
	{
		dist[i] = INF;
	}

	for (int i = 0; i < n; i++)
	{
		int node, out;
		cin >> node >> out;
		for (int j = 0; j < out; j++)
		{
			int c;
			cin >> c;
			G[node].push_back(c);
		}
	}


	queue<int> que;

	que.push(1);
	dist[1] = 0;

	while (!que.empty()) {

		int now = que.front();
		que.pop();

		if (dist[now] == INF)continue;
		for (int nxt : G[now]) {

			if (dist[nxt] > dist[now] + 1) {
				dist[nxt] = dist[now] + 1;
				que.push(nxt);
			}
		}
	}

	for (int i = 1; i <= n; i++)
	{
		if (dist[i] == INF) {
			cout << i << " " << -1 << endl;
		}
		else
		{
			cout << i << " " << dist[i] << endl;
		}
	}
	
	return 0;
}