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++: import java.util.LinkedList;
import java.util.Scanner;



public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int size = sc.nextInt();
		AdjListGraph g = new AdjListGraph(size, true);
		for (int i = 0; i < size; i++){
			int v = sc.nextInt()-1;
			int d = sc.nextInt();
			for(int j=0; j<d; j++){
				g.addEdge(v, sc.nextInt()-1);
			}
		}
		g.BFS(0);
	}

}


class Pair{
	int key;int value;
	Pair(int K, int V){key = K; value = V;}
	int getKey(){return key;}
	int getValue(){return value;}
}	

class AdjListGraph { // http://theoryofprogramming.com/adjacency-list-in-java/
	private boolean digraph;
	private final LinkedList<Pair>[] adjList;
	@SuppressWarnings("unchecked")
	AdjListGraph(int numV, boolean flag){
		adjList = (LinkedList<Pair>[]) new LinkedList[numV];
		for (int i = 0; i < adjList.length; i++) {
			adjList[i] = new LinkedList<>();
		}
		digraph = flag;
	}
	boolean directed(){return digraph;} //return the graph is directed or not
	int getNumOfVertices(){return adjList.length;} //return the num of vertices
	public void addEdge(int startV, int endV, int weight) {
		adjList[startV].add(new Pair(endV, weight));
		if(!digraph){adjList[endV].add(new Pair(startV, weight));}
	} //add an edge to the graph. 
	public void addEdge(int startV, int endV) {
		addEdge(startV, endV, 1); 
	} //weight is set to 1 as a default
	public LinkedList<Integer> getEdgesFromVertexNoWeight(int startV) {
		LinkedList< Integer > edgeList= (LinkedList< Integer >) new LinkedList<Integer>();
		for(Pair pair : adjList[startV]){
			edgeList.add(pair.getKey());
		}
		return edgeList;
	} // get the list of the edges leaving startV. This is for no weight graph. 
	public void print() {
		int i = 0;
		for (LinkedList<Pair> list : adjList) {
			System.out.print("adjList[" + i + "] -> ");
			for (Pair edge : list) {System.out.print(edge.getKey() + "(" + edge.getValue() + ")");}
			i++;
			System.out.println();
		}
	}
	void BFS(int src){
		boolean visited[] = new boolean[getNumOfVertices()];		
		int dist[] = new int[getNumOfVertices()];
		LinkedList<Integer> queue = new LinkedList<Integer>();
		visited[src]=true;	
		queue.add(src);	
		while (queue.size() != 0){
			int tmp = queue.poll();
			LinkedList<Integer> neighbors =  getEdgesFromVertexNoWeight(tmp);
			for(Integer i : neighbors){
				if(!visited[i]){
					visited[i] = true;
					dist[i] = dist[tmp]+1;
					queue.add(i);
				}
			}
		}
		for(int i=0; i< dist.length ; i++){
			if(i!=src && dist[i]==0){
				dist[i]=-1;
			}
		}
		for(int i=0; i< dist.length ; i++){
			System.out.println(Integer.toString(i+1) + " " +  dist[i]);
		}
//		System.out.println(Arrays.toString(dist));
	}
}

C++: #include <iostream>
#define INF (1<<25)
#define MAX 1001
using namespace std;

template <typename T>
class queue{
  private:
    T array[MAX];
    int head, tail, n;
  public:
    queue(int n){
      head=tail=0;
      this->n = n;
    }

    void push(T value){
      if (head == (tail+1)%n)
        return ;
      array[tail] = value;
      tail = (tail+1)%n;
    }

    bool empty(){
      if (head == tail)
        return true;
      return false;
    }

    void pop(void){
      if (head == tail) return;
      head = (head+1)%n;
    }

    T front(void){
      return array[head];
    }
};
int graph[MAX][MAX];
bool isDiscoverd[100];
int dist[100];
int dim[MAX];

void bfs(int i){
  queue<int> searchQueue(MAX);
  searchQueue.push(i);
  while(!searchQueue.empty()){
    auto current = searchQueue.front();
    int k = dim[current];
    searchQueue.pop();
    isDiscoverd[current] = true;
    for (auto ite=graph[current] ; ite != graph[current]+k ; ++ite){
      dist[*ite] = min(dist[*ite], dist[current]+1);
      if (!isDiscoverd[*ite]) searchQueue.push(*ite);
    }
  }
}

int main(){
  int n;
  cin >> n;
  for (int i=0;i<n;++i){
    int id;
    cin >> id;
    cin >> dim[id];
    for(int j=0;j<dim[id];++j){
      int temp;
      cin >> temp;
      graph[id][j] = temp;
      isDiscoverd[id]=false;
    }
  }

  dist[1] = 0;
  for (int i=2;i<=n;++i)
    dist[i] = INF;

  bfs(1);

  for (int i=1;i<=n;i++){
    cout << i << " " << (dist[i]>=INF?-1:dist[i]) <<endl;
  }

  return 0;
}

C++: 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <queue>

using namespace std;

const int MAX_N = 100;
int n;
int suji[MAX_N];
int	num[MAX_N];
int adj[MAX_N][MAX_N];
int ans[MAX_N][MAX_N];
int D[MAX_N];

void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;

	while(!Q.empty()){
		int cur = Q.front();
		Q.pop();

		for(int i = 1; i < n; i++){
			if(ans[cur][i] == 1 && D[i] == 0){
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}
}


int main(void){

	cin >> n;
	for(int i = 0; i < n; i++){
		
		cin >> suji[i] >> num[i];

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

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

	for(int i = 0; i < n; i++){
		for(int j = 0; j < num[i]; j++){
			ans[i][adj[i][j]] = 1;
		}
	}

	bfs(0);

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