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 <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=1<<21;
const int maxn=100+10;
int n;
vector<int> g[maxn];
int vis[maxn];
int d[maxn];
void bfs(int s)
{
    queue<int> que;
    que.push(s);
    vis[s]=1;
    d[s]=0;
    while(!que.empty())
    {
        int u=que.front();que.pop();
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(!vis[v])
            {
                que.push(v);
                vis[v]=1;
                d[v]=min(d[v],d[u]+1);

            }
        }
    }
}
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int no,cnt,dd;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&no,&cnt);
        for(int j=0;j<cnt;j++)
        {
            scanf("%d",&dd);
            g[no-1].push_back(dd-1);
        }
    }
    for(int i=0;i<n;i++) d[i]=INF;
    bfs(0);
    for(int i=0;i<n;i++)
    {
        printf("%d %d\n",i+1,d[i]==INF?-1:d[i]);
    }

    return 0;
}

C++: 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#include <cmath>
#define INF 10000000000
#define MOD 10000000007
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;
typedef pair<int, int> i_i;

int n;
int A[105][105];
vector<int> D(105,-1);

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    D[src] = 0;
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
        for (int dst=0; dst<n; dst++) {
            if (A[cur][dst]==1 and D[dst]==-1) {
                D[dst] = D[cur]+1;
                Q.push(dst);
            }
        }
    }
}

int main(){
    memset(A, 0, sizeof(A));
    cin >> n;
    for (int i=0; i<n; i++) {
        int u,k;
        cin >> u >> k;
        u--;
        for (int j=0; j<k; j++) {
            int num;
            cin >> num;
            A[u][num-1]=1;
        }
    }
    bfs(0);
    for (int i=0; i<n; i++) {
        cout << i+1 <<" "<<D[i]<<endl;
    }/*
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cout << A[i][j]<<" ";
        }
        cout << endl;
    }*/
}

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

using namespace std;

int G[100][100];
int D[100];
void init() {
	for (int i = 0; i < 100; i++) {
		for(int j = 0; j < 100; j++)
			G[i][j] = 0;
		D[i] = -1;
	}	
}

void bfs(int src);

int main() {
	init();
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int v_j;
			cin >> v_j;
			G[u - 1][v_j - 1] = 1;
		}
	}
	bfs(0);
	for (int i = 0; i < n; i++)
		cout << i + 1 << " " << D[i] << endl;
}

void bfs(int src) {
	D[src] = 0;
	queue<int> Q;
	Q.push(src);
	while (!Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int dst = 0; dst < 100; dst++)
			if (G[cur][dst] == 1 && D[dst] == -1){
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

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

    int n;
    cin >> n;
    vector<vector<int>> graph(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;
            graph[u].emplace_back(v);
        }
    }

    vector<int> dist(n, INF);
    queue<int> que;
    dist[0] = 0;
    que.push(0);
    while(!que.empty()){
        int from = que.front();
        que.pop();
        for(int to : graph[from]){
            if(chmin(dist[to], dist[from] + 1)){
                que.push(to);
            }
        }
    }

    for(int i = 0; i < n; ++i){
        if(dist[i] == INF)  dist[i] = -1;
        cout << i + 1 << ' ' << dist[i] << '\n';
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(ll i=0;i<N;++i)
typedef long long int ll;
ll INF = 1LL<<60;

ll N;
vector<vector <int> > M(110,vector<int>(110)); 
bool visit[110];
vector<ll> d(110,INF);

void bfs(int s){
    queue<int> que;
    que.push(s);
    d[s] = 0;

    while(!que.empty()){
        int v = que.front(); que.pop();
        for(int u = 0;u < N;u++){
            if(M[v][u] == 0) continue;
            if(d[u] != INF) continue;
            if(visit[u] == false){
                d[u] = d[v] + 1;
                que.push(u);
            }
        }
    }
}

int main(){
    cin >> N;
    rep(i,N){
        int u,k; cin >> u >> k;
        u--;
        rep(j,k){
            int v; cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0);

    rep(i,N){
        ll dist;
        (d[i] == INF) ? dist = -1 : dist = d[i];
        cout << i+1 << " " << dist << endl;
    }
}