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) {
			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 NIL=-1;

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

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

    queue<int> Q;
    Q.push(0);
    d[0]=0;
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        for(int v=0;v<n;v++){
            if(M[u][v]&&d[v]==NIL){
                Q.push(v);
                d[v]=d[u]+1;
            }
        }
    }

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

C++: #define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<functional>
#include<algorithm>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<map>
#include<utility>
#include<string>
#include<vector>

using namespace std;

#define LL long long
const int dx[4] = { 1,-1,0,0 };
const int dy[4] = { 0,0,1,-1 };
bool OOA(int x,int y,int H,int W){return (0<=x&&x<H)&&(0<=y&&y<W);}

struct Graph{
	int V,E;
	vector<int>cost;
	vector<bool>vzt;
	vector<vector<int>>adj;
	void init(int dflt){//dflt!=0
		adj=vector<vector<int>>(V+1);
		cost=vector<int>(V+1,dflt);
		vzt=vector<bool>(V+1,false);
	}

	void bfs(int s, int scost){
		queue<pair<int,int>>q;
		q.push({scost,s});
		vzt[s]=true;
		cost[s]=0;
		while(!q.empty()){
			auto top=q.front();
			q.pop();
			int cst=top.first,now=top.second;
			for(int i=0;i<adj[now].size();i++){
				if(!vzt[adj[now][i]]){
					vzt[adj[now][i]]=true;
					cost[adj[now][i]]=cost[now]+1;
					q.push({cost[adj[now][i]],adj[now][i]});
				}
			}
		}
	}
};

int n;
Graph G;

int main() {
	cin>>n;
	G.V=n;
	G.init(-1);
	for(int i=0;i<n;i++){
		int u,k;
		cin>>u>>k;
		for(int j=0;j<k;j++){
			int v;
			cin>>v;
			G.adj[u].push_back(v);
		}
	}
	G.bfs(1,0);
	for(int i=1;i<=G.V;i++)cout<<i<<" "<<G.cost[i]<<endl;
	return 0;
}

C++: #include <iostream>
#include <limits>

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] = INT_MAX;
  }
  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;
  }

 

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define MAX 105
#define BLACK 0
#define GRAY 1
#define WHITE 2

int n;
int G[MAX][MAX];
int d[MAX], color[MAX];

void init();
void bfs();

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

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

    bfs();

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

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

void bfs(){
    queue<int> Q;
    int now = 0;
    color[now] = GRAY; d[now] = 0;
    Q.push(now);

    while (!Q.empty()){
        for(int i = 0; i < n; i++){
            if(color[i] == WHITE && G[now][i] == 1){
                Q.push(i); color[i] = GRAY; d[i] = d[now]+1;
            }
        }
        Q.pop(); now = Q.front(); color[now] = BLACK;
    }

}