Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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

using namespace std;

int n;
int g[101][101] = {};
int sp[101] = {};


void bfs(){

  queue<int> q;
  q.push(1);

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


int main(){

  memset(g,0,sizeof(g));
  memset(sp,-1,sizeof(sp));
  cin >> n;
  sp[1] = 0;

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

  bfs();

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

}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int INF = (1<<29);
int ms[100][100],n;
int d[100];


void dfs(int a){
    rep(i,n){
        d[i]=INF;
    }
    queue<int> q;
    q.push(a);
    d[a]=0;
    while(!q.empty()){
        int b=q.front();q.pop();
        for(int i=0;i<n;i++){
            if(ms[b][i]!=0&&d[i]==INF){
                d[i]=d[b]+1;
                q.push(i);
                
            }
        }
    }
    
    
    
    rep(i,n){
        cout<<i+1<<" "<<(d[i]==INF?-1:d[i])<<endl;
    }
}


int main(){
    cin>>n;
    rep(i,n){
        int a,b,c;
        cin>>a>>b;
        a--;
        rep(j,b){
            cin>>c;
            c--;
            ms[a][c]=1;
        }
    }
    
    dfs(0);
    return 0;

    
}