Go: package main

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

type Queue []int

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

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

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

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: //1933-
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define INT(x) int x; scanf("%d",&x)
#define INPUT(x) scanf("%d",&x)
#define REP1(x,n) for(int x = 0; x < n; x++)
#define REP2(x,s,e) for(int x = s; x <= e; x++)
#define BR printf("\n")
#define INF 2000000000
#define FIND 1
#define END 2

vector<int> V[100];
priority_queue<pair<int,int> > PQ;//distance,id
int d[100];
int StateNode[100] = {};

void BFS(pair<int,int> p){
    if(d[p.second] <= -p.first) {
        PQ.pop();
        if(!PQ.empty()) BFS(PQ.top());
        return;
    }
    d[p.second] = -p.first;
    PQ.pop();
    
    REP1(k,V[p.second].size()){
        if (StateNode[V[p.second][k]] == 0 || (StateNode[V[p.second][k]] == FIND && -p.first+1)) {
            PQ.push(make_pair(p.first-1,V[p.second][k]));
            StateNode[V[p.second][k]] = FIND;
        }
    }
    
    if(!PQ.empty()) BFS(PQ.top());
}

int main(){
    INT(N);
    REP1(i,N){
        int from,n;
        scanf("%d %d",&from,&n);
        REP1(j,n){
            INT(to);
            V[i].push_back(to-1);
        }
        d[i] = INF;
    }
    
    PQ.push(make_pair(0,0));
    BFS(PQ.top());
    
    REP1(i,N){
        if (d[i] != INF) {
            printf("%d %d\n",i+1,d[i]);
        }else{
            printf("%d %d\n",i+1,-1);
        }
    }
    
    return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <bitset>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

struct dd{
    int id,f;
    dd(){
        id=0;
        f=-1;
    }
    dd(int a,int b){
        id=a;
        f=b;
    }
};
bool data[101][101]={};

void f1(dd*,int,int&,int);

int main()
{
    int N,M,num1,num2;
    dd ddata[101]={};
    cin>>N;
    int val[101]={};
    for(int i=0;i<N;i++){
        cin>>num1;
        cin>>M;
        val[num1]=-1;
        for(int j=0;j<M;j++){
            cin>>num2;
            data[num1][num2]=true;
        }
    }
    int time=1;
    queue<dd> que;
    bool vd[101]={};
    que.push(dd(1,0));
    vd[1]=true;
    for(dd ddata=que.front();!que.empty();que.pop(),ddata=que.front()){
        val[ddata.id]=ddata.f;
        for(int i=1;i<=N;i++){
            if(data[ddata.id][i]&&!vd[i]){
                vd[i]=true;
                que.push(dd(i,ddata.f+1));
            }
        }
    }
    for(int i=1;i<=N;i++){
        cout<<i<<' '<<val[i];
        cout<<endl;
    }
}

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

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

  StDist(int nDist) : m_nDist(nDist), m_nFromMeTo(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();
    if (rvoDist[nx].m_nFromMeTo)  continue; 

    rvoDist[nx].m_nFromMeTo = 1;
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
      if (cnrvvnAdjcy[nx][ny])
      {
        int nDist = rvoDist[nx].m_nDist + 1;
        if (rvoDist[ny].m_nDist == nIMPOS)
          rvoDist[ny].m_nDist = nDist;
        else
          rvoDist[ny].m_nDist = min(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 <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <memory>
#include <vector>
#include <queue>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG


struct Vertex {
  Vertex() :
    id_(-1),
    visited_(0),
    distance_(-1),
    adjNum_(-1) { }

  int id_;
  bool visited_;
  int distance_;
  int adjNum_;
  unique_ptr<int[]> adjIds_;

  void set(int id, vector<int> ids);
  void printInfo();
  void printDistance();
};


void Vertex::set(int id, vector<int> ids)
{
  id_ = id;
  adjNum_ = ids.size();

  if (adjNum_ == 0)
    return;

  adjIds_ = unique_ptr<int[]>(new int[adjNum_]);

  for (int i = 0; i < adjNum_; i++)
    adjIds_[i] = ids[i];
}


void Vertex::printInfo()
{
  DPRINTF("%d %d", id_, adjNum_);
  for (int i = 0; i < adjNum_; i++)
    printf(" %d", adjIds_[i]);
  cout << endl;
}


void Vertex::printDistance()
{
  printf("%d %d\n", id_, distance_);
}


void bfs(Vertex vertices[])
{
  const int startId = 1;
  queue<int> idQueue;

  idQueue.push(startId);

  int distance = 0;

  while (!idQueue.empty()) {

    DPRINTF("queue size %lu\n", idQueue.size());

    const size_t numOfThisDist = idQueue.size();

    for (size_t i = 0; i < numOfThisDist; i++)
    {
      int id = idQueue.front();
      idQueue.pop();

      DPRINTF("%zu  id %d\n", i, id);

      Vertex &vtx = vertices[id];

      if (vtx.visited_)
        continue;

      vtx.visited_ = true;
      vtx.distance_ = distance;

      for (int j = 0; j < vtx.adjNum_; j++)
        idQueue.push(vtx.adjIds_[j]);

    }

    distance++;
  }
}


int main() {
  int n;
  cin >> n; cin.ignore();

  DPRINTF("%d\n", n);

  // do not use [0], it's empty
  Vertex vertices[n+1];

  for (int i = 1; i <= n; i++)
  {
    int node, adjNum;

    cin >> node >> adjNum;

    vector<int> adjs;

    for (int j = 0; j < adjNum; j++)
    {
      int v;
      cin >> v;
      adjs.push_back(v);
    }

    vertices[i].set(node, adjs);

    cin.ignore();

  }

#if DEBUG
  for (int i = 1; i <= n; i++)
    vertices[i].printInfo();
#endif // DEBUG

  bfs(vertices);

  for (int i = 1; i <= n; i++)
    vertices[i].printDistance();

  return 0;

}