Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

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;
}