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;
int main(void){
	int n;
	int u,k,v;
	int box[101][101];
	int d[101];
	int i,j;
	int x,y;
	queue<int>que;
	
	cin>>n;
	for(i=1;i<=n;i++){
		d[i]=0;
		for(j=1;j<=n;j++){
			box[i][j]=0;
		}
	}
	for(i=0;i<n;i++){
		cin>>u>>k;
		for(j=0;j<k;j++){
			cin>>v;
			box[u][v]=1;
		}
	}
	d[1]=0;
	que.push(1);
	while(!que.empty()){
		x=que.front();
		que.pop();
		for(i=1;i<=n;i++){
			if(box[x][i]==1){
				y=i;
				if(d[y]==0){
					d[y]=d[x]+1;
					que.push(y);
				}
			}
		}
	}
	cout<<1<<" "<<d[i]<<endl;
	for(i=2;i<=n;i++){
		if(d[i]==0)	d[i]=-1;
		cout<<i<<" "<<d[i]<<endl;
	}
	return 0;
}

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

using namespace std;

int D[110]; // ?????¢????¨???¶
int v, A[100][100], u, k, w;
queue<int> Q;



int main(){
	cin >> v;
	// ???????????§??????v
	for (int i = 0; i < v; ++i) {

		cin >> u >> k;
		// ??????????????????u???k?????\?????????????????°??????

		for (int j = 0; j < k; ++j) {

			cin >> w;
			A[u - 1][w - 1] = 1;

			// ??£??\???????????§?¨??????????????????????????????????

		}
	}
		Q.push(0);
		D[0] = 0;

		while (!Q.empty()) {
			int cur = Q.front();
			Q.pop();
			for (int i = 0; i < v; ++i) {
				if (A[cur][i] == 1 && D[i] == 0) {
					D[i] = D[cur] + 1;
					Q.push(i);
				}
			}
		}

		for (int l = 0; l < v; ++l) {
			//????????????????????????????????????
			if (D[l] == 0) { D[l] = -1; }
			if (l == 0) { D[l] = 0; }
			cout << l + 1 << " " << D[l] << endl;
		}

	}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={};
int R[110][110]={};
int n,a,b,k;
void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;
	while (! Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for(int dst=1;dst<n+1;dst++){
			if(R[cur][dst]==1 && D[dst]==0){
				D[dst] =D[cur]+1;
				Q.push(dst);
			}
		}
	}
}
int main() {
	cin >> n;
	for(int i=0;i<n;i++){
		cin>>a;
		cin>>k;
		for(int j=0;j<k;j++){
		cin>>b;
		R[a][b]=1;
		}
	}
	bfs(1);
	cout <<"1 0"<<endl;
	for(int m=2;m<n+1;m++){
		if(D[m]==0){
		cout <<m<<" "<<-1<<endl;
		}else{
		cout <<m<<" "<<D[m]<<endl;
	}
	}
	return 0;
}

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

int main() {

	int n;
	std::cin >> n;
	std::vector<std::vector<int>> v(n);

	for (int i{}; i < n; ++i) {
		int u, k;
		std::cin >> u >> k;
		for (int j{}; j < k; ++j) {
			int t;
			std::cin >> t;
			v[u - 1].emplace_back(t - 1);
		}
	}


	std::vector<int> dist(n, 1<<25 );
	dist[0] = 0;
//	for (int i{0}; i < n; ++i) {
		std::queue<int> q;
		q.emplace(0);
		while (!q.empty()) {
			auto here = q.front(); q.pop();
			
			for (const auto&e : v[here]) {
				if (e!=0&&dist[e] > dist[here] + 1) {
					dist[e] = dist[here] + 1;
					q.emplace(e);
				}
			}

		}
	

	for (int i{}; i < n; ++i) {
		std::cout
			<< i + 1<< " "
			<<( dist[i] == (1 << 25) ? -1 : dist[i] )<< "\n";
	}

	return 0; 
}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={0};
void bfs(int src,int n,int g[110][110]){
queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
    int cur =Q.front();
    Q.pop();
    for(int r=0;r<n;r++){
        if (g[cur][r]==1&&D[r]==0){
            D[r]=D[cur]+1;
            Q.push(r);
        }
    }
}
}
int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
    cin>>t;
    g[k][t-1]=1;
}
}
bfs(0,n,g);

for(int a=0;a<n;a++){
    if (D[a]==0){
        D[a]=-1;
}
    D[0]=0;
    cout<<a+1<<" "<<D[a]<<endl;
}
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
#define N 100
#define INF (1<<21)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define rep(i, n) for(int i = 0; i < (n); ++i)

int n, M[N][N];
int color[N], d[N];


void bfs(int r)
{
    //initialization
    rep(i, n) color[i] = WHITE;
    rep(i, n) d[i] = INF;

    queue<int> Q;
    Q.push(r);
    color[r] = GRAY;
    d[r] = 0;

    int u;
    while(!Q.empty()){
        u = Q.front(); Q.pop();
        for (int v = 0; v < n; ++v){
            if (M[u][v] != 0 && d[v] == INF){
                d[v] = d[u] + 1;
                Q.push(v);
            }
        }
    }
    for(int i = 0; i < n; ++i){
        cout << i+1 << " " << ((d[i] == INF)? -1 : d[i]) << endl;
    }

}

int main()
{
    int u, k, v;

    scanf("%d", &n);
    for(u = 0; u < n; ++u){
        scanf("%d %d", &u, &k);
        --u;
        for(int j = 0; j < k; ++j){
            scanf("%d", &v);
            --v;
            M[u][v] = 1;
        }
    }

    bfs(0);

    return 0;
}