Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

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

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

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<100;i++){
        for(int j=0;j<100;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

    for(int i=0;i<n;i++){
        dis_min[i]=-1;
    }

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";

    //output
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<std::endl;
    }

    return 0;
}

C++: /**
 * Bredth first search.
 * graphs could be circular.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N] = {0};
int s[N] = {0};

void bfs(int n, int dd) {
	int k = adj[n][0];
	int kk = 0;
	for (int j=1;j<=k;j++) {
		int nn = adj[n][j];
		if (d[nn] != -1) continue;
		d[nn]=dd;
		s[kk++]=nn;
	}

	for (int j=0;j<kk;j++)
	{
		bfs(s[j], dd+1);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		d[j]=-1;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
	}

	d[1]=0;

	bfs(1, 1);

	for (int i=1;i<=n;i++){
		cout << i << ' ' << d[i] << endl;
	}

	return 0;
}

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::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<bits/stdc++.h>

using namespace std;

typedef vector<int> vec;
typedef vector<vec> Graph;

Graph G(110);

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		int u,t;
		scanf("%d%d",&u,&t);
		for(int j=0;j<t;j++)
		{
			int v;
			scanf("%d",&v);
			G[u].push_back(v);
		}
	}
	queue<int> que;
	int d[110];
	fill(d+1,d+n+1,-1);
	d[1]=0;
	que.push(1);
	while(!que.empty())
	{
		int t=que.front();que.pop();
		for(int i=0;i<G[t].size();i++)
			if(d[G[t][i]]==-1||d[t]+1<d[G[t][i]])
			{
				d[G[t][i]]=d[t]+1;
				que.push(G[t][i]);
			}
	}
	for(int i=1;i<=n;i++)
		printf("%d %d\n",i,d[i]);
	return 0;
}