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 <queue>
#define MAX 100
#define INF 2000000
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

int n;
int color[MAX+1];
int m[MAX+1][MAX+1];
int d[MAX+1];

void bfs(int s){
    queue<int> q;
    int u, v;
    for(u = 1; u <= n; u++){
        color[u] = WHITE;
        d[u] = INF;
    }
    color[s] = GRAY;
    d[s] = 0;
    q.push(s);
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(v = 1; v <= n; v++){
            if(m[u][v] == 0 || d[v] != INF) continue;
            if(color[v] == WHITE){
                color[v] = GRAY;
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
        color[u] = BLACK;
    }
}

int main(){
    int k, u, v, i, j;
    
    cin >> n;

    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++) m[i][j] = 0;
    }

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

    bfs(1);
    
    for(i = 1; i <= n; i++){
        cout << i << " ";
        if(d[i] == INF) cout << -1 << endl;
        else cout << d[i] << endl;
    }
    
    return 0;
}

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

int gra[101][101];
int dis[101];
int used[101];
int tt[101][2];
int n, a, b, c;



int main(){
	cin>>n;
	int minp, point,minv;
	for(int i=0; i<n; i++){
 		for(int j=1; j<=n; j++){
 			gra[i][j] = 99999;
		}
 	}
 	for(int i=0; i<n; i++){
 		cin>>a>>b;
		for(int j=1; j<=b; j++){
  			cin>>c;
 			gra[a][c]=1;
 		}
 	}
 	point = 1, gra[1][1] = 0;
 	for(int i=1; i<=n; i++){
 		dis[i] = gra[1][i];
	}
	for(int i=0; i<n-1; i++){
		used[point] = 1;
		minv = 99999;
		for(int j=1; j<=n; j++){
			if(gra[point][j]==1 && used[j]==0 && dis[point]+1<dis[j]){
				dis[j] = dis[point] + 1;
			}
			if(dis[j]<minv && dis[j]!=99999 && used[j]==0){
				minv = dis[j], minp = j;
			}			
		}
		point = minp;		
	}
 	cout<<"1 "<<dis[1]<<"\n"; 	
 	for(int i=2; i<=n; i++){
 		cout<<i<<" ";
 		if(dis[i]==99999)cout<<"-1\n";
		else cout<<dis[i]<<"\n";
	}
 
 return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N, Dist[105];
vector <int> Graph[105];

int BFS(){
  fill(Dist, Dist + N + 1, -1);
  queue <int> que; que.push(1);
  Dist[1] = 0;
  while (!que.empty()){
    int Q = que.front(); que.pop();
    for (int i = 0; i < Graph[Q].size(); i++){
      int next = Graph[Q][i];
      if (Dist[next] != -1) continue;
      Dist[next] = Dist[Q] + 1;
      que.push(next); 
    }
  }  
  return 0;
}

int main(){
  scanf("%d", &N);
  for (int i = 0; i < N; i++){
    int U, K; scanf("%d%d", &U, &K);
    for (int j = 0; j < K; j++){
      int V; scanf("%d", &V);
      Graph[U].push_back(V);
    }
  }
  BFS();
  for (int i = 1; i <= N; i++){
    printf("%d %d\n", i, Dist[i]);
  }
  
  return 0;
}

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 <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 100000

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(d[v]!=INFTY)continue;
      if(M[u][v]==1){
        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;
}