Go: package main

/*
グラフの表現 隣接行列による表現
*/

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
 
struct status{
    int idx_node;
    vector<int> children;
    int visited = 0;
    int depth = 0;
};
 
deque<int> schedule; 
vector<status> graph;

int depth = 1;
void bfs(){
    if (schedule.size()>1){
        
        int now_node = schedule.front();
        schedule.pop_front();
        
        switch(now_node){
            case 0: {
                ++depth;
                schedule.push_back(0);
                break;
            }
            default: {
                for(auto& next_node:graph[now_node-1].children){
                    if(graph[next_node-1].visited) continue;
                    
                    graph[next_node-1].visited = 1;
                    graph[next_node-1].depth = depth;  
                    schedule.push_back(next_node);
                }
            }
        }
        bfs();
    }
}
 

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //stdi
    int nb_nodes; cin >> nb_nodes;
    graph.resize(nb_nodes);
    for(auto& times:graph){
        int idx_node; cin >> idx_node; graph[idx_node-1].idx_node = idx_node;
        int nb_edges; cin >> nb_edges; graph[idx_node-1].children.resize(nb_edges);
        for(auto& a:graph[idx_node-1].children){
            cin >> a;
        }
        sort(graph[idx_node-1].children.begin(),graph[idx_node-1].children.end());
    }
    
    //bfs
    schedule.push_back(1);
    schedule.push_back(0);
    bfs();
     
    //stdo
    for(auto& a:graph){
        cout<<a.idx_node<<" "
            <<a.depth<<"\n";
    }
}

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;
    }
}