Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

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

bool A[110][110],flag[110];
int d[110];
int n;

void bfs(){
	queue<int> Q;
	Q.push(1);
	d[1]=0;
	while(Q.empty()==0){
		int u=Q.front();
		Q.pop();
		for(int i=2;i<=n;i++){
			if(A[u][i]==true && flag[i]==false ){
				flag[i]=true;
				Q.push(i);
				d[i]=d[u]+1;
			}
		}
	}
	return ;
}

int main(){
	int a,b,c;
	cin>>n;
	for(int i=1;i<=n;i++){
		flag[i]=false;
			for(int j=1;j<=n;j++){
				A[i][j]=false;
		}
	}
	for(int i=1;i<=n;i++){
		cin>>a>>b;
		for(int j=1;j<=b;j++){
			cin>>c;
			A[a][c]=true;
		}
	}
	bfs();
	for(int i=1;i<=n;i++) cout<<i<<" "<<d[i]<<endl;	
	return 0;
}