Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

C++: #include <iostream>
#include <cstring>

using namespace std;

int adja[101][101], dist[101];

void bfs() {
    int que[100], l = 0, d = 1;
    que[0] = 1;l++;
    while (l > 0) {
        int temp[100], m = 0;
        for (int i = 0; i < l; ++i) {
            int u = que[i];
            if (0 < dist[u] && dist[u] <= d)
                continue;
            dist[u] = d;
            for (int j = 0, v; (v = adja[u][j]) > 0; j++) {
                temp[m++] = v;
            }
        }
        memcpy(que, temp, sizeof(int) * m);
        l = m;
        d++;
    }
}

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int main() {
    ll n;
    cin >> n;
    vector<vector<ll>> G(n);
    rep(i, n) {
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            ll v;
            cin >> v;
            v--;
            G[u].emplace_back(v);
        }
    }
    vector<ll> dist(n, -1);
    dist[0] = 0;

    queue<ll> que;
    que.push(0);

    while(!que.empty()) {
        auto v = que.front();
        que.pop();

        for(auto nv : G[v]) {
            if (dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i, n) cout << i+1 << " " << dist[i] << endl;
}

C++: #include <bits/stdc++.h>
#include<math.h>
#define rep(i,n) for (int i = 0; i < (n) ; ++i)
using namespace std;
using ll = long long ;
using P = pair<int , int> ;
#define PI 3.14159265358979323846264338327950
#define INF 1e18 

int main() {
    int n ;
    cin >> n ;
    vector<int> list [n] ;
    rep(i, n){
        int v, k;
        cin >> v >> k ;
        v-- ;
        rep(j, k){
            int g ;
            cin >> g  ;
            g-- ;
            list[v].push_back(g) ;
        }
    }
    queue<P> que ;
    vector<int> d (n, -1) ;
    que.push(P(0, 0)) ;
    while(!que.empty()) {
        P st = que.front() ;
        que.pop() ;
        if(d[st.first] > -1){
            continue ;
        }
        else {
            int f = st.first ;
            int e = st.second ;
            d[f] = e ;
            for(int u : list[f]){
                que.push(P(u, e + 1)) ;
            }
        }
 
    }
    rep(i, n){
        cout << i + 1 << " " << d[i] << endl ;
    }
}

C++: #include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
typedef long long ll;
typedef pair<int,int> ip;
typedef pair<ll,ll> lp;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

int main(){
	int n;
	cin>>n;
	vector<int> ans(110,-1);
	vector<vector<int>> input (110);
	REP(i,n){
		int el_siz;
		cin>>el_siz>>el_siz;
		vector<int> el(el_siz);
		rep(j,el_siz)cin>>el[j];
		input[i] = el;
	}
	queue<int> Q;
	int cnt = 0;
	ans[1] = 0;
	Q.push(1);
	while(!Q.empty()){
		int node = Q.front();
		Q.pop();
		rep(i,input[node].size()){
			if(ans[input[node][i]] == -1){
				ans[input[node][i]] = ans[node]+1;
				Q.push(input[node][i]);
			}
		}
	}
	REP(i,n)cout<<i<<' '<<ans[i]<<endl;
	return 0;
}

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

int n;

int V[100][100];
int D[100];
int d = 0;

std::queue<int> Q;

void bfs(int r) {
    Q.push(r);
    D[r] = 0;
    while (Q.size()) {
        r = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++) {
            if ((D[i] == -1) && (V[r][i] == 1)) {
                Q.push(i);
                D[i] = D[r] + 1;
            }
        }
    }
}


int main() {
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        int id, e_num;
        std::cin >> id >> e_num;
        for (int j = 0; j < e_num; j++) {
            int t;
            std::cin >> t;
            V[id - 1][t - 1] = 1;
        }
    }

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

    bfs(0);
    for (int i = 0; i < n; i++) {
        std::cout << i + 1 << " " << D[i] << std::endl;
    }
}