Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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


#define show(x) #x " = " << x

int n;
vector<int> G[100];
int d[100];

int main() {
	int i;
	cin >> n;
	for ( i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for( i = 0; i < k; i++) cin >> v, G[u - 1].push_back(v - 1);
	}
	for (i = 0; i < 100; i++) d[i] = -1;

	queue<int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int v = q.front(); q.pop();
		for (int i = 0; i < G[v].size(); i++) {
			int x = G[v][i];
			if (d[x] == -1) {
				d[x] = d[v] + 1;
				q.push(x);
			}
		}
	}
	for (i = 0; i < n; i++) cout << i + 1 << " " << d[i] << endl;

}

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