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 <stdio.h>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;
 
int main(void) {
  int n, ans = 0, inf = 10000;
  scanf("%d", &n);
  int dis[n];
  rep(i, n) dis[i] = inf;
  dis[0] = 0;
  vector< int > vec[n];
  rep(i, n) {
    int u, k;
    scanf("%d%d", &u, &k);
    u--;
    rep(j, k) {
      int v;
      scanf("%d", &v);
      v--;
      vec[u].push_back(v);
    }
  }
  rep(i, n - 1) {
    rep(j, n) {
      vector< int >::iterator it;
      for(it = vec[j].begin(); it != vec[j].end(); ++it) {
        if(dis[*it] > dis[j] + 1) dis[*it] = dis[j] + 1;
      }
    }
    /*rep(j, n) printf("%d ", dis[j]);
    printf("\n");*/
  }
  rep(i, n) {
    if(dis[i] != inf) printf("%d %d\n", i + 1, dis[i]);
    else printf("%d -1\n", i + 1);
  }
  return 0;
}

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

using namespace std;

const int MAX = 100;
int adj[MAX][MAX] = {};

class BFS{
    int num, d[MAX], status[MAX];
public:
    BFS(int n) : num(n){
        for(int i=0;i<n;i++){
            d[i] = -1;
            status[i] = 0;
        }
    }

    void run(int node){
        queue<int> Q;
        Q.push(node);
        d[node] = 0;
        status[node] = 1;
        while(!Q.empty()){
            node = Q.front(); Q.pop();
            for(int i=0;i<num;i++){
                if(adj[node][i] && status[i] == 0) {
                    Q.push(i);
                    d[i] = d[node] + 1;
                    status[i] = 1;
                }
            }
        }
    }

    void print(){
        for(int i=0;i<num;i++){
            cout << i+1 << " " << d[i] << endl;
        }
    }
};

int main(){
    int r, c, n_c, n;
    cin >> n;
    BFS bfs(n);

    for(int i=0;i<n;i++){
        cin >> r >> n_c;
        for(int j=0;j<n_c;j++){
            cin >> c;
            adj[r-1][c-1]++;
        }
    }

    bfs.run(0);
    bfs.print();
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace  std;

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nDmy;

  StDist(int nDist) : m_nDist(nDist), m_nDmy(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
    {
      if (!cnrvvnAdjcy[nx][ny])          continue;
      if (rvoDist[ny].m_nDist != nIMPOS) continue; 

      int nDist = rvoDist[nx].m_nDist + 1;
      rvoDist[ny].m_nDist = nDist;
      rqnBFSrch.push(ny);
    }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}

C++: #include<cstdio>
#include<queue>
using namespace std;

const int INF = -1;

int G[110][110] = {};
int d[110] = {};

void bfs(int s, int n)
{
  int v;
  queue<int> que;

  for(int i = 1;i <= n;i++) d[i] = INF; //init

  que.push(s);
  d[s] = 0;
  
  while(que.size()){
    v = que.front();
    que.pop();

    for(int i = 1;i <= n;i++){
      if(G[v][i] != 1) continue;
      if(d[i] != INF) continue;

      que.push(i);
      d[i] = d[v]+1;
    }
  }
}

int main()
{
  int n;
  int i,j;
  int u,k,v;

  scanf("%d", &n);

  for(i = 0;i < n;i++){
    scanf("%d%d", &u, &k);

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

  for(i = 1;i <= n;i++)
    if(d[i] == 0) bfs(i, n);

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

  return 0;
}

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, k, v;
	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>
using namespace std;

vector<int> graph[101];
int n;

void bfs(int start){
	vector<int> d(n+1,-1);
	d[1]=0;

	queue<int> Q;
	Q.push(start);

	while(Q.size()){
		int u=Q.front();	Q.pop();
		for(int v:graph[u]){
			if(d[v]>=0)continue;

			d[v]=d[u]+1;
			Q.push(v);
		}
	}

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

	return;
}

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


	bfs(1);

	return 0;
}