Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

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

const int Max = 100;
const int white = 0;
const int black = 1;
int M[Max][Max];
int d[Max], nt[Max], color[Max];
int n;
queue<int> Q;

int next(int u){
    for(int v=nt[u]; v<n; v++){
        nt[u] = v + 1;
        if(M[u][v]) return v;
    }
    return -1;
}

void bfs_visit(int u){
    for(int i=0; i<n; i++){
        nt[i] = 0;
        d[u] = 0;
    }

    color[u] = black;
    Q.push(u);
    while(!Q.empty()){
        int u = Q.front();
        int v = next(u);
        if(v != -1){
            if(color[v] == white){
                color[v] = black;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        } else {
            Q.pop();
        }
    }
}

int main(){
    int u, k, v;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        color[i] = white;
    }
    for(int i=0; i<n; i++){
        cin >> u >> k;
        u--;
        for(int i=0; i<k; i++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs_visit(0);

    for(int i=0; i<n; i++){
        cout << i+1 << " " << d[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]=0;
    }

    //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";
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<"\n";
    }
    return 0;
}

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

const long N = 100;
const long INFTY = (1<<21);

long n, M[N + 1][N + 1];
long d[N + 1];

void bfs(long s){
	queue<long> q;
	q.push(s);
	for (long i = 1; i <= n; i++) d[i] = INFTY;
	d[s] = 0;
	long u;
	while (!q.empty()){
		u = q.front();
		q.pop();
		for (long v = 1; v <= n; v++){
			if (M[u][v] == 0) continue;
			if (d[v] != INFTY) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	for (long i = 1; i <= n; i++){
		cout << i << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
	}
}

int main(){
	long u, k, v;
	cin >> n;
	for (long i = 1; i <= n; i++){
		for (long j = 1; j <= n; j++){
			M[i][j] = 0;
		}
	}
	for (long i = 0; i < n; i++){
		cin >> u >> k;
		for (long j = 0; j < k; j++){
			cin >> v;
			M[u][v] = 1;
		}
	}

	bfs(1);

	return 0;
}

C++: // Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <iostream>
#include <queue>
#include <limits.h>

using namespace std;
static const int NMAX = 100;
int N, D[NMAX] = {};
bool G[NMAX][NMAX] = { { false } };

void breadthFirstSearch(int base) {
	int ix;
	for (int i = 0; i < N; i++) {
		D[i] = INT_MAX;
	}

	D[base] = 0;
	queue<int> q;
	q.push(base);
	while (!q.empty()) {
		ix = q.front(); q.pop();
		for (int i = 0; i < N; i++) {
			if (G[ix][i]) {
				q.push(i);
				if (D[ix] + 1 < D[i]) D[i] = D[ix] + 1;
			}
		}
	}

	for (int i = 0; i < N; i++) {
		cout << i + 1 << " " << ((D[i] == INT_MAX) ? (-1) : D[i]) << endl;
	}
}

int main() {
	int ix, deg, cache;
	cin >> N;

	for (int i = 0; i < N; i++) {
		cin >> ix >> deg;
		for (int j = 0; j < deg; j++) {
			cin >> cache;
			G[ix-1][cache-1] = true;
		}
	}

	breadthFirstSearch(0);

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <stdio.h>

using namespace std;

int n, u, k, v;
int adjList[101][101];
int adjNum[101];
bool visited[101];

int lv[101];

queue<int> q;

void bfs(int entry_node) {
    lv[entry_node] = 0;
    q.push(entry_node);

    while (!q.empty()) {
        u = q.front(); q.pop();
        visited[u] = true;

        for (int i = 0; i < adjNum[u]; i++) {
            if (!visited[adjList[u][i]]) {
                if (lv[adjList[u][i]] == -1) {
                    lv[adjList[u][i]] = lv[u] + 1;
                }
                q.push(adjList[u][i]);
            }
        }
    }

}

int main () {
	scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        adjNum[u-1] = k;
        for (int j = 0; j < k; ++j) {
            scanf("%d", &v);
            adjList[u-1][j] = v-1; // all IDs start from 1
        }

        visited[u-1] = false;
        lv[u-1] = -1; // set lv to -1 by default
    }

    bfs(0);
    for (int u = 0; u < n; ++u) {
        cout << u+1 << " " << lv[u] << "\n";
    }

	return 0;
}