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 <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<iostream>
#include<queue>
#include<cstring>
using namespace std;

int k[1000][1000], n, a, b, c, t, x[1000];
queue<int>Q;
queue<int>R;

int main() {
	memset(x, -1, sizeof(x));
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		for (int j = 0; j < b; j++) {
			cin >> c;
			k[i][c] = 1;
		}
	}
	Q.push(1);
	x[1] = 0;
	while (!Q.empty()) {
		t++;
		while (!Q.empty()) {
			for (int i = 1; i <= n; i++) {
				if (x[i] == -1 && k[i][c] == 1) {
					x[i] = t;
					R.push(i);
				}
			}
			Q.pop();
		}
		while (!R.empty()) {
			Q.push(R.front());
			R.pop();
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << i << ' ' << x[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
//#include "../cxx-prettyprint/prettyprint.hpp"


int main() {
    int n;
    std::cin >> n;

    std::unordered_map<int, std::vector<int>> Adj(n);

    for (int i = 0; i < n; i ++) {
        int u, k;
        std::cin >> u >> k;
        std::vector<int> V(k);
        for (int& v : V) {
            std::cin >> v;
        }
        Adj[u] = V;
    }

    std::set<int> nodes_unvisited;
    std::unordered_map<int, int> distances;

    for (int i = 1; i <= n; i ++) {
        nodes_unvisited.insert(i);
    }

    nodes_unvisited.erase(1);
    distances[1] = 0;
    std::queue<int> queue;
    queue.push(1);

    while (!queue.empty()) {
        int u = queue.front();
        queue.pop();
        for (int v : Adj[u]) {
            if (nodes_unvisited.find(v) != nodes_unvisited.end()) {
                nodes_unvisited.erase(v);
                distances[v] = distances[u] + 1;
                queue.push(v);
            }
        }
    }

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

C++: #include<cstdio>
#include<algorithm>

using namespace std;

class Graph{
    int n;
    int **G;
    int *seen;
    int *todo;
    int *next;
    int *dist;
    const int MAX = 101;
    public: Graph(int _n){
        n = _n;
        G = new int*[n+1];
        seen = new int[n+1];
        todo = new int[n+1];
        next = new int[n+1];
        dist= new int[n+1];
        for(int i = 0;i <=n;++i){
            G[i] = new int[n+1];
            seen[i] = 0;
            todo[i] = 0;
            next[i] = 0;
            dist[i] = MAX;
        }
        for(int i = 0;i <= n;++i){
            for(int j = 0;j <= n;++j){
                G[i][j] = 0;
            }
        }
    }
    
    public: void BuildMatrix(){
        int u,k,v;
        for(int i = 0;i < n;++i){
            scanf("%d %d",&u,&k);
            for(int j = 0;j < k;++j){
                scanf("%d",&v);
                G[u][v] = 1;
            }
        }
    }
    
    public: void PrintMatrix(){
        for(int i = 1;i <= n;++i){
            for(int j = 1;j <n;++j){
                printf("%d ",G[i][j]);
            }
            printf("%d\n",G[i][n]);
        }
    }
    
    
   public: void DoBFS(){
        dist[1] = 0;
        todo[1] = 1;
        while(!isEmpty(todo)){
            for(int i = 1;i <= n;++i){
                if(todo[i] == 1) BFS(i,dist[i]);
            }
            for(int i = 1;i <= n;++i) swap(todo[i],next[i]);
        }
        for(int i = 1;i <= n;++i) printf("%d %d\n",i,(dist[i] != MAX) ? dist[i]:-1);
    }
    void BFS(int s,int cnt){
        cnt++;
        todo[s] = 0;
        seen[s] = 1;
        for(int i = 1;i <= n;++i){
            if(G[s][i]  == 1){
                dist[i] = std::min(dist[i],cnt);
                if(seen[i] != 1) next[i] = 1;
            }
        }
    }
    
    bool isEmpty(int* l){
        for(int i = 1;i <= n;++i){
            if(l[i] != 0) return false;
        }
        return true;
    }
    
};

int main(){
    int n;
    scanf("%d",&n);
    Graph G(n);
    G.BuildMatrix();
    G.DoBFS();
}

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

using namespase 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];
    }
  }

  bfs(0);

  return 0;
}