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++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <tuple>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <random>
#include <cstdlib>
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;

vector<int> V[105];
queue<int> Q;
vector<int> color(105, 1), d(105, 0);

int main() {
	int n;
	cin >> n;
	Loop(i, n) {
		int u, k;
		cin >> u >> k;
		Loop(j, k) {
			int v;
			cin >> v;
			V[u].push_back(v);
		}
	}
	//
	Q.push(1);
	color[1] = 2;
	while (Q.size() > 0) {
		int u = Q.front();
		Q.pop();
		Loop(i, V[u].size()) {
			if (color[V[u][i]] == 1) {
				Q.push(V[u][i]);
				color[V[u][i]] = 2;
				d[V[u][i]] = d[u] + 1;
			}
		}
	}
	Loop1(i, n) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

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

int n;
vector< vector<int> > G;
vector<int> color;
vector<int> dis;

void Bfs(int start) {
  queue<int> q;
  q.push(start);
  color[start]++;
  dis[start] = 0;

  while (q.empty() == false) {
    int u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (G[u][v] == 1 && color[v] == 0) {
        color[v]++;
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
    color[u]++;
  }
}

int main() {
  cin >> n;
  G.resize(n, vector<int>(n));
  color.resize(n);
  dis.resize(n, -1);
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      v--;
      G[u][v]++;
    }
  }

  Bfs(0);

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

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

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

  StDist(int nDist) : m_nDist(nDist), m_nDmy(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();
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
    {
      if (!cnrvvnAdjcy[nx][ny])          continue;
      if (rvoDist[ny].m_nDist != nIMPOS) continue; 

      int nDist = rvoDist[nx].m_nDist + 1;
      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 <iostream>
#include <vector>
#include <queue>

using namespace std;

vector<int> graph[100];
int dist[100]={0};
int main(){
  int n;
  cin >> n;

  for(int i=0;i<n;++i){
    int u,k;
    cin >> u >> k;
    for(int i=0;i<k;++i){
      int v;
      cin >> v;
      graph[u-1].push_back(v-1);
    }
    dist[i] = -1;
  }

  queue<int> q;
  q.push(0);
  dist[0] = 0;
  while(!q.empty()){
    auto v = q.front();q.pop();
    for(auto next_v:graph[v]){
      if(dist[next_v]==-1){
        dist[next_v] = dist[v]+1;
        q.push(next_v);
      }
    }
  }
  for(int i=0;i<n;++i){
    cout << i+1 << " " << dist[i] << endl;
  }
}

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

using namespace 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]=1;
    }
  }

  bfs(0);

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>

static const int N = 101;

using namespace std;

int main () {
  int n, v, v2, k;
  int d[N], visited[N];
  vector<vector<int> > V;
  queue<int> Q;
  int maxNumber = 1 << 21;

  for (int i = 0; i <= N; i++) {
    vector<int> A;
    V.push_back(A);
  }
  for (int i = 0; i <= N; i++) {
    d[i] = maxNumber;
    visited[i] = 0;
  }

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> v >> k;

    for (int j = 0; j < k; j++) {
      cin >> v2;
      V[v].push_back(v2);
    }
  }

  Q.push(1);
  d[1] = 0;

  while (Q.size()) {
    for (vector<int>::iterator it = V[Q.front()].begin(); it != V[Q.front()].end(); it++) {
      if (!visited[*it]) {
        Q.push(*it);
        //printf("d[%d] = d[%d] + 1\n", *it, Q.front());
        d[*it] = min(d[*it], d[Q.front()] + 1);
        visited[*it] = 1;
      }
    }

    Q.pop();
  }

  for (int i = 1; i <= n; i++)
    cout << i << " " << (d[i] == maxNumber ? -1 : d[i]) << endl;

  return 0;
}