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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <iomanip>
#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 INF 100000
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
vector<vector< int> > G;
int N;
vector<int> flag;
vector<int> d;
queue<int> que;

void bfs(int k){
    if(flag[k] == -1){
        que.push(k);
        flag[k] = 0;
        while(!que.empty()){
            int now = que.front(); que.pop();
            if(G[now].size() != 0){
                REP(i,G[now].size()){
                    if(flag[G[now][i]] == -1){
                        flag[G[now][i]] = flag[now] + 1;
                        que.push(G[now][i]);
                    }
                }
            }
        }
    }
}

int main(){
    int n;
    int time = 0;

    
    cin >> n;
    G.resize(n);
    d.resize(n);//最初に発見された時刻
    flag.resize(n,-1);
    REP(i,n){
        int u,k; cin >>u>>k;
        u--;
        if(k != 0){
        REP(j,k){
            int a; cin >>a;
            G[u].push_back(a);
            G[u][j]--;
        }
        }
    }
    //time = 0;
    bfs(0);

    REP(i,n){
        cout << i + 1 << " " << flag[i] << endl;
    }
}