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<queue>

using namespace std;

static const int N=100;
static const int INFTY=(1<<21);

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

void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=0; i<n; i++){
        d[i]=INFTY;
    }
    d[s]=0;
    int u;
    while(!q.empty()){
        u=q.front(); 
        q.pop();
        for(int v=0; v<n; v++){
            if(M[u][v]==0){continue;}
            if(d[v]!=INFTY){continue;}
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    for(int i=0; i<n; i++){
        cout<<i+1<<" "<<((d[i]==INFTY)? (-1):d[i])<<endl;
    }
}

int main(){
    int u,v,k;
    cin>>n;

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            M[i][j]=0;
        }
    }

    for(int i=0; i<n; i++){
        cin>>u>>k;
        u--;
        for(int j=0; j<k; j++){
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;


void bfs(v2& a, v2& b){
	queue<int> q;
	q.push(a[0][0]);
	while(!q.empty()){
		int x = q.front()-1;
		int t = b[x][1];
		q.pop();
		rep(i, a[x][1]){
			int y = a[x][2+i]-1;
			if(a[y][0] != -1 && a[y][0] != -2){
				b[y][1] = t+1;
				a[y][0] = -1;
			}
			if(a[y][0] != -2) q.push(y+1);
		}
		a[x][0] = -2;
	}
}

int main(){
	int n;
	cin >> n;
	v2 a(n, v1(0));
	v2 b(n, v1(2, 0));
	rep(i, n){
		int m;
		cin >>m;
		a[i].push_back(m);
		b[i][0] = m;
		int x;
		cin >> x;
		a.at(i).push_back(x);
		rep(j, x){
			int y;
			cin >> y;
			a.at(i).push_back(y);
		}
	}
	bfs(a, b);
	for(int i = 1;i<n; i++){
		if(b[i][1] == 0) b[i][1] = -1;
	}
	rep(i, n){
		rep(j, 2){
			cout << b[i][j];
			if(j != 1) cout << " ";
			else cout << endl;
		}
	}
}

C++: #include<stdio.h>
#include<queue>
#define MAX 101
using namespace std;

int G[MAX][MAX], d[MAX];
queue<int> Q;

void adjacency_matrices(int n) {
    int i, j, u, m, k;

    for (i=1; i<=n; i++) {
        scanf("%d", &u);
        scanf("%d", &m);
        for (j=1; j<=m; j++) {
            scanf("%d", &k);
            G[u][k] = 1;
        }
    }
}

void breadth_first_search(int n, int i, int dist) {
    int k;
    d[i] = dist++;
    for (int j=1; j<=n; j++) {
        if (G[i][j]==1 && d[j]==-1) {
            Q.push(j);
            d[j] = dist;
        }
    }

    while(!Q.empty()) {
        dist++;
        int length = Q.size();
        for (int j=1; j<=length; j++) {
            k = Q.front(); Q.pop();
            for (int m=1; m<=n; m++) {
                if (G[k][m]==1 && d[m]==-1) {
                    Q.push(m);
                    d[m] = dist;
                }
            }
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);

    for (int i=1; i<=n; i++) {
        d[i]=-1;
        for (int j=1; j<=n; j++) {
            G[i][j] = 0;
        }
    }

    adjacency_matrices(n);

    breadth_first_search(n, 1, 0);

    for (int i=1; i<=n; i++) {
        printf("%d %d\n", i, d[i]);
    }
}

C++: #include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;

using Graph = vector<int>;
vector<Graph>g;

int main() {
    int n;
    cin >> n;
    g.resize(n);
    vector<int>s(n, -1);
    for (int i=0; i < n; i++) {
        int num, count;
        cin >> num >> count;
        for (int j=0; j < count; j++) {
            int t;
            cin >> t;
            g[num-1].emplace_back(t-1);
        }
    }

    queue<int> que;
    que.push(0);
    s[0] = 0;
    while (!que.empty()) {
        int tyouten = que.front();
        que.pop();
        Graph gu = g[tyouten];
        for (int i=0; i < gu.size(); i++) {
            int num = gu[i];
            if (s[num] == -1) {
                s[num] = s[tyouten] + 1;
                que.push(num);
            }
        }
    }

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

    return 0;
}

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

using namespace std;

int main() {
    int32_t n;
    cin >> n;

    vector<vector<int32_t>> A(n+1);

    for (int32_t i = 0; i < n; i++) {
        int32_t u, k;
        cin >> u >> k;

        for (int32_t j = 0; j < k; j++) {
            int32_t v;
            cin >> v;

            A[u].push_back(v);
        }
    }

    vector<bool> visited(n+1);
    fill(visited.begin(), visited.end(), false);
    vector<int32_t> d(n+1);
    fill(d.begin(), d.end(), -1);
    queue<int32_t> tovisit;
    visited[1] = true;
    d[1] = 0;
    tovisit.push(1);

    while (!tovisit.empty()) {
        int32_t u = tovisit.front();
        tovisit.pop();

        for (int32_t v : A[u]) {
            if (!visited[v]) {
                visited[v] = true;
                d[v] = d[u] + 1;
                tovisit.push(v);
            }
        }
    }

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

    return 0;
}