Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

//---------------------------------------------------------------
// ------ graph ------ //
using Weight = int;
using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    Flow cap;
    Edge() : src(0), dst(0), weight(0) { }
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // pqが降順のため
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
void add_edge1(Graph &g, int a, int b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
}
void add_edge2(Graph &g, int a, int b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
    g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
}
//---------------------------------------------------------------

vector<int> bfs(int src, Matrix &M) {
    int n = M.size();
    vector<int> dist(n, -1);
    queue<int> Q;
    dist[src] = 0;
    Q.push(src);
    while (!Q.empty()) {
        int cur = Q.front(); Q.pop();
        for (int nxt = 0; nxt < n; nxt++) {
            if (M[cur][nxt] && dist[nxt] < 0) {
                dist[nxt] = dist[cur] + 1;
                Q.push(nxt);
            }
        }
    }
    return dist;
}

int main() {
    int n; cin >> n;
    Matrix M(n, Array(n, 0));
    for (int i = 0; i < n; i++) {
        int u, k; cin >> u >> k;
        while (k--) {
            int v; cin >> v; M[u - 1][v - 1]++;
        }
    }
    vector<int> dist = bfs(0, M);
    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << dist[i] << endl;
    }
    return 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;
}