Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include <algorithm>
#include <iostream>
#include <list>
#include <vector>

class TNode
{
	std::list<TNode*> FNexts;
	int  FDistance;
public:
	TNode()
	: FDistance(-1)
	{
	}
	
	int GetDistance()const {return FDistance;}
	
	void Push(TNode* New)
	{
		FNexts.push_back(New);
	}
	
	void Visit(int Distance)
	{
		if(FDistance >= 0 && Distance > FDistance) return;
		FDistance = Distance;
		for(std::list<TNode*>::iterator It = FNexts.begin(); It != FNexts.end(); ++It){
			(*It)->Visit(Distance + 1);
		}
	}
};

class TGraph
{
	std::vector<TNode*> FNodes;
public:
	TGraph(int Count)
	: FNodes(Count + 1)
	{
		for(int i = 1; i < Count + 1; ++i){
			FNodes[i] = new TNode();
		}
	}
	
	~TGraph()
	{
		for(unsigned int i = 1; i < FNodes.size(); ++i){
			delete FNodes[i];
		}
	}
	
	void InputNode()
	{
		int Index;
		std::cin >> Index;
		int BranchCount;
		std::cin >> BranchCount;
		for(int i = 0; i < BranchCount; ++i){
			int Target;
			std::cin >> Target;
			FNodes[Index]->Push(FNodes[Target]);
		}
	}
	
	void BreathFirstSearch()
	{
		FNodes[1]->Visit(0);
	}
	
	int Distance(int Target)const
	{
		return FNodes[Target]->GetDistance();
	}
};

int main()
{
	int Count;
	std::cin >> Count;
	TGraph Graph(Count);
	for(int i = 0; i < Count; ++i){
		Graph.InputNode();
	}
	Graph.BreathFirstSearch();
	
	for(int i = 0; i < Count; ++i){
		std::cout << i + 1 << " " << Graph.Distance(i + 1) << std::endl;
	}
	return 0;
}

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

int main() {
	int a[101][101] = {{0}};
	int n, u, k, v, km;
	km = 0;
	cin >> n;
	for ( int i = 0; i < n; i++ ) {
		cin >> u, k;
		for ( int j = 1; j <= k; j++ ) {
			cin >> v;
			a[u][v] = 1;
		}
		km++;
	}
	int n0;
	int b[101] = {- 1};
	queue<int> q;
	q.push( 1 );
	b[1] = 0;
	for ( int i = 1; i <= km; i++ ) {
		n0 = q.front();
		q.pop();
		for ( int j = 1; j <= km + 1; j++ ) {
			if ( a[n0][j] == 1 && b[j] == - 1 ) {
				q.push( j );
				b[j] = b[n0] + 1;
			}
			if ( q.empty() ) break;
		}
	}
	for ( int i = 1; i <= km; i++ )
		cout << i << " " << b[i] << endl;
}

C++: //Breadth First Search (?????????) Algorithm
#include<iostream>
#include<queue>
#define WH 0
#define GR 1
#define BL 2
#define INF 2000000
void bfs(int);
void init(void);
int n, *color, **M, *d;
std::queue<int> Q;
int main(){
  std::cin>>n;
  color=new int[n], d=new int[n], M=new int*[n];
  for(int i=0, u, k;i<n;i++){
    M[i]=new int[n];
    color[i]=WH, d[i]=INF;
    for(int j=0;j<n;j++) M[i][j]=0;
    std::cin>>u>>k;
    for(int j=0, v;j<k;j++){
      std::cin>>v;
      M[u-1][v-1]=1;
    }
  }
  bfs(0);
  for(int i=0;i<n;i++) std::cout<<i+1<<" "<<(d[i]!=INF?d[i]:-1)<<std::endl;
  delete color;
  delete d;
  for(int i=0;i<n;i++) delete M[i];
  delete M;
  return 0;
}

void bfs(int s){
  color[s]=GR, d[s]=0;
  Q.push(s);
  while(!Q.empty()){
    int u=Q.front();
    Q.pop();
    for(int i=0;i<n;i++){
      if(M[u][i]&&color[i]==WH){
        color[i]=GR, d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BL;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



int main(){
    int n;
    cin >> n;
    V<V<int>> g(n);
    rep(i,n) {
        int a;
        cin >> a;
        a--;
        int k;
        cin >> k;
        rep(j,k) {
            int b;
            cin >> b;
            b--;
            g[a].push_back(b);
        }
    }

    V<int> d(n,-1);
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while(sz(q)) {
        int v = q.front(); q.pop();
        for(int nv : g[v]) {
            if(d[nv] != -1) continue;
            d[nv] = d[v] + 1;
            q.push(nv);
        }
    }

    rep(i,n) {
        printf("%d %d\n", i+1, d[i]);
    }
}

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 <stdio.h>
#include <queue>
#include <algorithm>

#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

int n;
int M[N][N]; // ??£??\??????
int d[N]; // ???????????¢


void bfs()
{
    int i;
    for (i = 0; i < n; i++) {
        d[i] = -1;
    }
    
    int u;
    u = 0;
    d[u] = 0;
    queue<int> q;
    q.push(u);
    
    while (!q.empty()) {
        u = q.front();
        q.pop();
        for (i = 0; i < n; i++) {
            if (M[u][i] == 0) continue;
            q.push(i);
            if (d[i] == -1) {
                d[i] = d[u] + 1;
            }
        }
    }
    
    for (i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}


int main()
{
    scanf("%d", &n);
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }
    
    // input graph
    int u, k, v;
    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        for (j = 0; j < k; j++) {
            scanf("%d", &v);
            M[u-1][v-1] = 1;
        }
    }
    
    bfs();
    
    return 0;
}