Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

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

func (q *Queue) pop() Pair {
	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([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

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

	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>
#include<vector>
#include<algorithm>

class Graph{
private:





 class Node{
 private:
  unsigned long node_id_;
  long depth_;

  std::vector<unsigned long> adjnodes_;
 public:

  Node(const unsigned long node_id,const std::vector<unsigned long> &adjnodes)
      : node_id_(node_id),
        depth_(-1),
        adjnodes_(adjnodes){
   if(node_id_ == 1) depth_ = 0;
  };

  bool isDepth(){
   if(depth_ == -1) return true;

   return false;
  };

  void CheckNode(std::queue<unsigned long>& queue_nextnode,const std::vector<Node*>& node){
   for(unsigned long adjnode_i = 0, adjnode_max = adjnodes_.size(); adjnode_i < adjnode_max; ++adjnode_i){
    unsigned long nextadjnode = adjnodes_[adjnode_i]-1;
    if(node[nextadjnode]->isDepth()){
     node[nextadjnode]->depth_ = depth_ + 1;
     queue_nextnode.push(nextadjnode);
    }
   }
  };

  void display(){
   std::cout << node_id_ << " ";
   std::cout << depth_ << " ";
   std::cout << std::endl;
  };
 protected:
 };





 std::vector<Node*> node_;

public:
 Graph(unsigned long node_max) : node_(std::vector<Node*>(node_max, 0)){};
 void CreateNode(const unsigned long node_i,const unsigned long node_id,const std::vector<unsigned long>& adjnodes){
  node_[node_i] = new Node(node_id, adjnodes);
 };

 void BreadthFirstSearch(){
  if(node_.empty()) return ;

  std::queue<unsigned long> queue_nextnode;
  queue_nextnode.push(0);
  while(!queue_nextnode.empty()){
   unsigned long nextnode = queue_nextnode.front();
   queue_nextnode.pop();
   node_[nextnode]->CheckNode(queue_nextnode, node_);

  }

 };

 void display(){
  for(unsigned long node_i = 0, node_max = node_.size(); node_i < node_max; ++node_i){
   node_[node_i]->display();
  }
 };
protected:

};


int main(){

 unsigned long node_max = 0; std::cin >> node_max;

 /*
  *  ノード数分の記憶領域を確保する。
  */
 Graph graph(node_max);

 /*
  *入力はテキスト。
  *入力は一行目に    [ノード数]
  *      二行目以降  [頂点id 接続ノード数 接続ノード1 接続ノード2 ...]
  */
 for(unsigned long node_i = 0; node_i < node_max; ++node_i){
  unsigned long node_id = 0; std::cin >> node_id;
  unsigned long adjnode_max = 0; std::cin >> adjnode_max;
  std::vector<unsigned long> adjnodes(std::vector<unsigned long>(adjnode_max, 0));
  for(unsigned long adjnode_i = 0; adjnode_i < adjnode_max; ++adjnode_i){
   std::cin >> adjnodes[adjnode_i];
  }

  graph.CreateNode(node_i, node_id, adjnodes);
 }

 graph.BreadthFirstSearch();
 graph.display();

 return 0;
};

C++: include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
#define int ll

int g[105][105], d[105], n;

void bfs() {
  queue<int> q;
  q.push(0);
  fill(d, d + n, -1);
  d[0] = 0; 

  while(!q.empty()) {
    int que = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (g[que][i] && d[i] == -1) {
        d[i] = d[que] + 1;
        q.push(i);
      }
    }
  }
}

signed main(void) {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      g[u-1][v-1] = 1;
    }
  }

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

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

int A[100][100],dk[100],L[100];

void BFS(int n,int u){
    int k;
    queue<int> Q;
    Q.push(u);
    dk[u]=0;L[u]=1;
    while(!Q.empty()){
        k=Q.front();Q.pop();
        for(int i=0;i<n;i++){
            if(A[k][i]==1&&L[i]==-1){
                Q.push(i);L[i]=1;dk[i]=dk[k]+1;
            }
        }
    }
}

int main(){
    int n,v,u,k;
    cin>>n;
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)A[i][j]=-1;
    for(int i=0;i<n;i++){L[i]=-1;dk[i]=-1;}

    for(int i=0;i<n;i++){
        scanf("%d %d",&v,&u);
        v--;
        for(int j=0;j<u;j++){
            scanf("%d",&k);
            k--;
            A[v][k]=1;
        }
    }
    BFS(n,0);
    for(int i=0;i<n;i++){
        printf("%d %d\n",i+1,dk[i]);
    }
}

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

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

#define NO -1

using namespace std;


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



void bfs(int o)
{
	queue<int> S;
	S.push(o);
	color[o] = GRAY;
	d[o] = 0;

	int fr;

	while (S.size() > 0) {
		fr = S.front();
		S.pop();
		color[fr] = BLACK;
		for (int i = 1; i <= n; i++) {
			if (color[i] == WHITE && adjacencyList[fr][i] != NO) {
				S.push(i);
				color[i] = GRAY;
				d[i] = d[fr] + 1;
			}
		}
	}

}

int main(void)
{

	int u;
	int k;
	int v;


	cin >> n;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			adjacencyList[i][j] = NO;
		}
	}

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

		for (int j = 0; j < k; j++) {
			cin >> v;
			adjacencyList[u][v] = 1;
		}

	}

	bfs(1);

	for (int i = 1; i <= n; i++) {
		cout << i << " ";
		if (color[i] == BLACK) {
			cout << d[i];

		}
		else {			
			cout << -1;
		}
		cout << endl;
	}


	return 0;
}

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

using namespace std;

struct Edge{
	int to;
};

void BFS(vector<vector<Edge>>& G)
{
	queue<int> Q;
	vector<int> depth(G.size(), -1);
	int dep = 0;
	
	depth[0] = dep;
	for(auto i : G[0])
		Q.push(i.to);
	while(!Q.empty()){
		dep++;
		int size = Q.size();

		for(int i = 0; i < size; i++){
			int v = Q.front();
			Q.pop();
			if(depth[v-1] == -1){
				depth[v-1] = dep;
				for(auto i : G[v-1])
					Q.push(i.to);
			}
		}
	}

	for(int i = 0; i < G.size(); i++){
		cout << i + 1 << " " << depth[i] << endl;
	}
}

int main(void)
{
	int num;
	cin >> num;

	vector<vector<Edge>> AdjList(num);
	for(int i = 0; i < num; i++){
		int u, k;
		cin >> u >> k;
		for(int j = 0; j < k; j++){
			int v;
			cin >> v;
			AdjList[i].push_back(Edge{v});
		}
	}

	BFS(AdjList);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> e[105];
int res[105];
bool ok[105];
int n;
int inf=1<<30;
int main(){
//	freopen("a.txt","r",stdin);
	cin>>n;
	int u,v,k;
	fill(res,res+103,inf);
	while(cin>>u){
		cin>>k;
		for(int i=0;i<k;i++)cin>>v,e[u].push_back(v);
	}
	queue<pair<int,int> >q;
	q.push({1,0});
	ok[1]=1;
	while(!q.empty()){
		pair<int,int> p=q.front();q.pop();
		int i=p.first;int d=p.second;
		res[i]=min(res[i],d);
		for(int j=0;j<e[i].size();j++)
			if(!ok[e[i][j]])q.push({e[i][j],d+1}),ok[e[i][j]]=1;
	}
	for(int i=1;i<=n;i++)
		printf("%d %d\n",i,(ok[i])?res[i]:-1);
	return 0;
}