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 <bits/stdc++.h>
using namespace std;

#define N_MAX 100
#define WH 0
#define BK 2


long n;
long G[N_MAX][N_MAX] = {};
long color[N_MAX] = {};
long dist[N_MAX];
queue<long> Q;

void bfs(long u) {
    Q.push(u);
    color[u] = BK;
    dist[u] = 0;
    while (Q.empty() == false) {
        u = Q.front();
        Q.pop();
        for (long v = 0; v < n; v++) {
            if (G[u][v] == 1 && color[v] == WH) {
                Q.push(v);
                color[v] = BK;
                dist[v] = dist[u] + 1;
            }
        }
    }
}

int main() {
    long v_from, deg_out, v_to;

    cin >> n;
    for (long i = 0; i < n; i++) {
        cin >> v_from >> deg_out;
        v_from--;
        for (long j = 0; j < deg_out; j++) {
            cin >> v_to;
            v_to--;
            G[v_from][v_to] = 1;
        }
    }

    // for (long i = 0; i < n; i++) {
    //     for (long j = 0; j < n; j++) {
    //         if (j != 0) { cout << " "; }
    //         cout << G[i][j];
    //     }
    //     cout << endl;
    // }


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

    bfs(0);

    for (long i = 0; i < n; i++) {
        cout << i + 1 << " " << dist[i] << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define MAX 100
using namespace std;
vector<int>adjlist[MAX+5];
int visited[MAX+5];
int dist[MAX+5];
int main()
{
    int n,x,u,k,y,g;
    cin>>n;
    memset(dist,-1,sizeof(dist));
    for(int i=1; i<=n; i++)
    {
        cin>>u>>k;
        for(int j=0; j<k; j++)
        {
            cin>>x;
            adjlist[u].push_back(x);
        }
    }
    queue<int>q;
        int i=1;
        dist[i]=0;
        if(visited[i]==0 && adjlist[i].size()!=0)
        {
            //cout<<i<<endl;
            q.push(i);
            visited[i]=1;
            while(!q.empty())
            {
                x=q.front();
                q.pop();
                for(int j=0; j<adjlist[x].size(); j++)
                {
                    g=adjlist[x][j];
                    if(visited[g]==0)
                    {
                        visited[g]=1;
                        dist[g]=dist[x]+1;
                        q.push(g);
                    }

                }
            }
        }

    for(int i=1; i<=n; i++)
    {
        cout<<i<<" "<<dist[i]<<endl;
    }

    return 0;

}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<utility>
using namespace std;
using pii=pair<int,int>;

int dist[102];
queue<pii> q;
vector<int> adj[105];
bool vis[105];

int main(){
	ios::sync_with_stdio(0);
	int n;
	cin>>n;
	for(int i=1,u,k;i<=n;i++){
		cin>>u>>k;
		for(int j=0,tt;j<k;j++){
			cin>>tt;
			adj[u].push_back(tt);
		}
	}
	
	q.push({1,0});
	dist[1]=0;
	for(int i=2;i<=n;i++) dist[i]=1e8;
	
	while(!q.empty()){
		int from=q.front().first, fw=q.front().second;
		q.pop();
		if(fw!=dist[from]) continue;

		for(int to:adj[from]){
			if(dist[to]>dist[from]+1){
				dist[to]=dist[from]+1;
				q.push({to,dist[from]+1});
			}
		}
	}
	
	for(int i=1;i<=n;i++){
		cout<< i <<' '<< ((dist[i]==1e8)?-1:dist[i]) <<endl;
	}
}

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

using namespace std;

int n;
int u[10010], k[10010],  v[10010][10010];
int answer[10010][10010];

int D[10010];
void bfs(int src){
	//cerr << "bfs root = " << src << endl;
	queue<int> Q;
	Q.push(src);
	D[src] = 0;
	//int cnt = 0;
	while(! Q.empty()){
		int cur = Q.front();
		Q.pop();
		//cerr << "visiting " << cur << " " << D[cur] << endl;
		
		for(int dst = 0; dst < 1001; ++dst){
			if(answer[cur - 1][dst - 1] == 1 && D[dst] > 10000){
				D[dst] = D[cur] + 1; 
				Q.push(dst);
			}
		}

	}
	for(int i = 1; i  < n + 1; ++i ){
		if(D[i] < 10000){
			cout << i << " " <<  D[i] << endl;
		}else{
			cout << i << " " << -1 << endl;
		}
	}

	
}

int main(){
	cin >> n;
	for(int i = 0; i < 1001; ++i){
		for(int j = 0; j < 1001; ++j)
			answer[i][j] = 0;
	}
	for(int i = 0; i < 10010; ++i){
		D[i] = 10010;
	}

	
	
	for(int i = 0; i < n; ++i){
		cin >> u[i] >> k[i];
		for(int j = 0; j < k[i]; ++j){
			cin >> v[i][j];
			answer[u[i] - 1][v[i][j] - 1] = 1;
		} 
	}
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			//cout << answer[i][j] << " ";
		} 
		//cout << endl;
	}
	bfs(1);
}

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
mat g;

lli u,k,v;

queue<lli> q;
queue<lli> d;
vbl vis;
vll ans;


int main(){
    cin >> n;
    g = mat(n+1);
    vis = vbl(n+1);
    ans = vll(n+1,-1);
    for(lli i = 0;i < n;i++){
        cin >> u >> k;
        for(lli j = 0;j < k;j++){
            cin >> v;
            g[u].push_back(v);
        }
    }
    q.push(1);
    d.push(0);
    while(!q.empty()){
        lli p = q.front();
        lli r = d.front();
        q.pop();
        d.pop();
        if(vis[p]) continue;
        vis[p] = true;
        ans[p] = r;
        for(lli i = 0;i < g[p].size();i++){
            if(!vis[g[p][i]]) q.push(g[p][i]),d.push(r+1);
        }
    }
    for(lli i = 1;i <= n;i++) cout << i << " " << ans[i] << endl;
    return 0;
}