Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

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

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

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			if !dl.isVisited(v) {
				stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
			}
		}
	}
}

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

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=1<<21;
const int maxn=100+10;
int n;
vector<int> g[maxn];
int vis[maxn];
int d[maxn];
void bfs(int s)
{
    queue<int> que;
    que.push(s);
    vis[s]=1;
    d[s]=0;
    while(!que.empty())
    {
        int u=que.front();que.pop();
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(!vis[v])
            {
                que.push(v);
                vis[v]=1;
                d[v]=min(d[v],d[u]+1);

            }
        }
    }
}
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int no,cnt,dd;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&no,&cnt);
        for(int j=0;j<cnt;j++)
        {
            scanf("%d",&dd);
            g[no-1].push_back(dd-1);
        }
    }
    for(int i=0;i<n;i++) d[i]=INF;
    bfs(0);
    for(int i=0;i<n;i++)
    {
        printf("%d %d\n",i+1,d[i]==INF?-1:d[i]);
    }

    return 0;
}

C++: 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#include <cmath>
#define INF 10000000000
#define MOD 10000000007
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;
typedef pair<int, int> i_i;

int n;
int A[105][105];
vector<int> D(105,-1);

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=0; dst<n; dst++) {
            if (A[cur][dst]==1 and D[dst]==-1) {
                D[dst] = D[cur]+1;
                Q.push(dst);
            }
        }
    }
}

int main(){
    memset(A, 0, sizeof(A));
    cin >> n;
    for (int i=0; i<n; i++) {
        int u,k;
        cin >> u >> k;
        u--;
        for (int j=0; j<k; j++) {
            int num;
            cin >> num;
            A[u][num-1]=1;
        }
    }
    bfs(0);
    for (int i=0; i<n; i++) {
        cout << i+1 <<" "<<D[i]<<endl;
    }/*
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cout << A[i][j]<<" ";
        }
        cout << endl;
    }*/
}

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

using namespace std;

int G[100][100];
int D[100];
void init() {
	for (int i = 0; i < 100; i++) {
		for(int j = 0; j < 100; j++)
			G[i][j] = 0;
		D[i] = -1;
	}	
}

void bfs(int src);

int main() {
	init();
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int v_j;
			cin >> v_j;
			G[u - 1][v_j - 1] = 1;
		}
	}
	bfs(0);
	for (int i = 0; i < n; i++)
		cout << i + 1 << " " << D[i] << endl;
}

void bfs(int src) {
	D[src] = 0;
	queue<int> Q;
	Q.push(src);
	while (!Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int dst = 0; dst < 100; dst++)
			if (G[cur][dst] == 1 && D[dst] == -1){
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<vector<int>> graph(n);
    for(int i = 0; i < n; ++i){
        int u, k;
        cin >> u >> k;
        --u;
        for(int j = 0; j < k; ++j){
            int v;
            cin >> v;
            --v;
            graph[u].emplace_back(v);
        }
    }

    vector<int> dist(n, INF);
    queue<int> que;
    dist[0] = 0;
    que.push(0);
    while(!que.empty()){
        int from = que.front();
        que.pop();
        for(int to : graph[from]){
            if(chmin(dist[to], dist[from] + 1)){
                que.push(to);
            }
        }
    }

    for(int i = 0; i < n; ++i){
        if(dist[i] == INF)  dist[i] = -1;
        cout << i + 1 << ' ' << dist[i] << '\n';
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(ll i=0;i<N;++i)
typedef long long int ll;
ll INF = 1LL<<60;

ll N;
vector<vector <int> > M(110,vector<int>(110)); 
bool visit[110];
vector<ll> d(110,INF);

void bfs(int s){
    queue<int> que;
    que.push(s);
    d[s] = 0;

    while(!que.empty()){
        int v = que.front(); que.pop();
        for(int u = 0;u < N;u++){
            if(M[v][u] == 0) continue;
            if(d[u] != INF) continue;
            if(visit[u] == false){
                d[u] = d[v] + 1;
                que.push(u);
            }
        }
    }
}

int main(){
    cin >> N;
    rep(i,N){
        int u,k; cin >> u >> k;
        u--;
        rep(j,k){
            int v; cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0);

    rep(i,N){
        ll dist;
        (d[i] == INF) ? dist = -1 : dist = d[i];
        cout << i+1 << " " << dist << endl;
    }
}