Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

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_ << " ";
  };
 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();
   if(node_i != node_max-1)std::cout << std::endl;
  }
 };
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;
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<k;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 <queue>
using namespace std;
static const int N = 100;
static const int INF = (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] = INF;
	}
	d[s] = 0;
	int u;
	while (!q.empty()) {
		u = q.front();
		q.pop();
		for (int v = 0; v < n; v++) {
			if (M[u][v] == 1 && d[v] == INF) {
				d[v] = d[u] + 1;
				q.push(v);
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i+1 << " " << ( (d[i] == INF) ? -1 : d[i]) << endl;
	}
}
int main(int argc, char *argv[]) {
	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 <bits/stdc++.h>
using namespace std;
bool A[105][105],V[105]={0};
int d[105],n;
queue<int> B;

void bfs(){
	B.push(1);
	d[1] = 0;
	V[1] = 1;
	while(!B.empty()){
		int v = B.front();
		B.pop();
		for(int i=1;i<=n;i++)
			if(A[v][i] && !V[i]){
				d[i] = d[v]+1;
				V[i] = 1;
				B.push(i);
			} 
	}
}
int main()
{	
	memset(A,0,sizeof(A));
	int v,u,du;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>v>>du;
		while(du--)
			cin>>u,
			A[v][u] = 1;
	}
	bfs();
	for(int i=1;i<=n;i++)
		if(!V[i]) d[i] = -1;
	for(int i=1;i<=n;i++)
		printf("%d %d\n",i,d[i]);
	return 0;
}