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>
const int MAX_N = 100;
const int INF = 999999999;
using namespace std;

int n;
int color[MAX_N];
bool adj[MAX_N][MAX_N];
int d[MAX_N];

void bfs(int s) {
    queue<int> que; que.push(s);
    for (int i = 0; i < n; i++) d[i] = INF;
    d[s] = 0;

    int u;
    while (!que.empty()) {
        u = que.front(); que.pop();
        for (int v = 0; v < n; v++) {
            if (adj[u][v] && d[v] == INF) {
                d[v] = d[u] + 1;
                que.push(v);
            }
        }
    }
}
int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            adj[i][j] = false;
        }
    }

    for (int i = 0; i < n; i++) {
        int u, k; cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v; cin >> v;
            adj[u-1][v-1] = true;
        }
    }

    bfs(0);

    for (int i = 0; i < n; i++) {
        if (d[i] != INF) {
            cout << i+1 << " " << d[i] << endl;
        } else {
            cout << i+1 << " " << -1 << endl;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <stack>
#include <climits>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;

vector<int> G[105];
int d[105];

int main()
{
    int n; cin >> n;
    for(int i = 0; i< n; i++){
        int u,k;
        cin >> u >> k;
        u--;
        for(int j = 0; j < k; j++){
            int x; cin >> x;
            x--;
            G[u].push_back(x);
        }
    }
    for(int i = 0; i < 105; i++){
        d[i] = -1;
    }
    queue<int> q;
    d[0] = 0;
    q.push(0);
    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int i = 0; i < G[u].size(); i++){
            int nv = G[u][i];
            if(d[nv] == -1){
                d[nv] = d[u] + 1;
                q.push(nv);
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout << i+1 << ' ' << d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX = 101;
int n,m[MAX][MAX],d[MAX];
queue<int> s;

void bfs(int num)
{
	for(int i=1;i<=n;i++) d[i]=INT_MAX;
	s.push(num);
	d[num]=0;
	while(!s.empty())
	{
		int u=s.front();
		s.pop();
		for(int i=1;i<=n;i++)
		{
			if(m[u][i]==0) continue;
			if(d[i]!=INT_MAX) continue;
			s.push(i);
			d[i]=d[u]+1;
		}
	}
	for(int i=1;i<=n;i++) cout<<i<<" "<<( d[i]==INT_MAX ? (-1): d[i] )<<endl;
}
int main()
{
	int u,k,t;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>u>>k;
		for(int j=0;j<k;j++)
		{
			cin>>t;
			m[u][t]=1;
		}
	}
	bfs(1);
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------

int n;
bool g[110][110];
int d[110];

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

	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = 1; i <= n; i++) {
			if (g[u][i] && d[i] == -1) {
				q.push(i);
				d[i] = d[u] + 1;
			}
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			g[u][v] = true;
		}
	}

	for (int i = 1; i <= n; i++) d[i] = -1;

	bfs(1);

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

	return 0;
}

C++: #include<stdio.h>
#include<queue>
using namespace std;
int tu[100][100];
int a[100];
int d[100];
int n;
void bfs(int x){
    queue<int> q;
    q.push(x);
    for(int i=0;i<n;i++)d[i]=10000000;
    d[x]=0;
    int u;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int i=0;i<n;i++){
            if(tu[u][i]==0)continue;
            if(d[i]!=10000000)continue;
            d[i]=d[u]+1;
            q.push(i);
        }
        }
        for(int i=0;i<n;i++){
            printf("%d ",i+1);
            if(d[i]==10000000)printf("-1");
            else printf("%d",d[i]);
            printf("\n");
        }
}
int main(){
    scanf("%d",&n);
    int v,m,k;
    for(int i=0;i<n;i++){
        scanf("%d%d",&v,&m);
        v--;
        for(int j=0;j<m;j++){
            scanf("%d",&k);
            k--;
            tu[v][k]=1;
        }
    }
    bfs(0);
    return 0;
}

C++: #include <iostream>


using namespace std;

bool adjmatrix[101 * 101];
int costs[101];



void BFS(int vertex, int cur_cost, int N) {
  if (costs[vertex] < cur_cost) return;
  costs[vertex] = cur_cost;
  for (int i =1; i<= N; i++) {
    if(adjmatrix[vertex*101 + i]) {
      BFS(i, cur_cost + 1, N);
    }
  }
}
  

int main() {
  for (int i = 0; i < 101; i++) {
    costs[i] = 101;
  }
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    int vertex, degree;
    cin >> vertex >> degree;

    for (int j = 0; j < degree; j++) {
      int outvertex;
      cin >> outvertex;
      adjmatrix[vertex * 101 + outvertex] = true;
    }
  }


  /*
  //function to print adjmatrix
  for (int i = 0; i <= N; i++) {
    for (int j= 0; j <= N; j++) {
      if (adjmatrix[i * 101 + j]) {
	cout << "1 ";
      } else {
	cout << "0 ";
      }
    }
    cout << endl;
    }*/

  BFS(1, 0, N);

  for (int i =1; i<=N; i++) {
    cout << i << " " << costs[i] << endl;
  }

 

}