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 <vector>
#include <list>
#include <cstdlib>

const int MAX_N = 100;

struct Node {
  int v;
  int d;
  bool visited;
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);  

  int n = 0;
  std::cin >> n;

  std::vector<std::vector<int> *> *graph = new std::vector<std::vector<int> *>(n + 1);
  std::vector<struct Node *> *nodes = new std::vector<struct Node *>(n + 1);
  
  for (int i = 1; i <= n; i++) {
    int v = 0;
    std::cin >> v;

    int k = 0;
    std::cin >> k;

    std::vector<int> *neighbors = new std::vector<int>(k);

    for (int i = 0; i < k; i++) {
      int a = 0;
      std::cin >> a;

      neighbors->at(i) = a;
    }

    graph->at(i) = neighbors;

    struct Node *node = (struct Node *) std::malloc(sizeof(struct Node));

    node->v = i;
    node->d = -1;
    node->visited = false;
    
    nodes->at(i) = node;
  }

  int count = 0;
  int start = 1;

  std::list<int> *stack = new std::list<int>();

  stack->push_back(nodes->at(start)->v);

  while (!stack->empty()) {
    std::vector<int> *newNodes = new std::vector<int>();

    while (!stack->empty()) {    
      int v = stack->front();
      stack->pop_front();

      nodes->at(v)->d = count;
      nodes->at(v)->visited = true;
      
      std::vector<int> *neighbors = graph->at(v);

      for (std::vector<int>::iterator iterator = neighbors->begin(); iterator != neighbors->end(); ++iterator) {
	struct Node *node = nodes->at(*iterator);

	if (!node->visited) {
	  newNodes->push_back(node->v);
	}
      }
    }

    for (std::vector<int>::iterator iterator = newNodes->begin(); iterator != newNodes->end(); ++iterator) {
      stack->push_front(*iterator);
    }

    count++;
  }

  for (std::vector<struct Node *>::iterator iterator = nodes->begin() + 1; iterator != nodes->end(); ++iterator) {
    struct Node *node = *iterator;
        
    std::cout << node->v << " " << node->d << std::endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <queue> 
using namespace std;
int G[100][100];
int d[100];
int color[100];
int n;
queue<int> qu;
void bfs ()
{
    color[0] = 1;
    d[0] = 0;
    qu.push(0);
    while (!qu.empty())
    { 
        int u = qu.front(); qu.pop();
        for (int i = 0; i < n; i++)
        {
            if (G[u][i] == 1 && color[i] == 0)
            {
                color[i] = 1;
                d[i] = d[u] + 1;
                qu.push(i);
            }
        }
        color[u] = 2;
    } 
    return;
}

int main()
{
    int k, u, v;
    cin>>n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            G[i][j] = 0; 
        }  
    } 

    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;
        } 
    }

    for (int i = 0; i < n; i++)
    {
        d[i] = -1; 
        color[i] = 0;
    } 
    bfs();
    for (int i = 0; i < n; i++)
    {
         printf("%d %d\n", i+1, d[i]);
    } 
    return 0;
}

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

const int MAXINT = 1000;

void bfs(std::vector<std::vector<int> >& adj_list, std::vector<int>& d, int root = 0 ){
  std::queue<int> target_q;
  // std::queue<int> dist_q;
  target_q.push(root);
  d[root] = 0;
  // dist_q.push(0;)
  while(!target_q.empty()){
    int vtx = target_q.front(); target_q.pop();
    for(int const&  u : adj_list[vtx]){
      if(d[vtx] + 1 < d[u]){
      target_q.push(u);
        // std::cout<< "/* message */" << std::endl;
        d[u] = d[vtx] + 1;
      }
    }
  }
}


int main(int argc, char const *argv[]) {
  int n; // .. 100
  std::cin >> n;
  std::vector<std::vector<int> > adj_list(n);
  std::vector<int> d(n, MAXINT);

  for(int q=0; q<n; ++q){
    int i_plus_1, k;
    std::cin >> i_plus_1;
    std::cin >> k;
    for(int p=0; p<k; ++p){
      int j_plus_1;
      std::cin >> j_plus_1;
      // adj[i_plus_1-1][j_plus_1-1] = true;
      adj_list[i_plus_1 - 1].push_back(j_plus_1-1);
    }
  }
  bfs(adj_list, d, 0);

  for(int i=0; i<n; ++i){

    // if(d[i] < MAXINT){
      std::cout << (i+1)
        << " " << (d[i]==MAXINT ? -1 : d[i])
        << std::endl;
  }


  // for(auto it = adj_list.begin(); it != adj_list.end(); ++it){
  //   std::cout << ": ";
  //   for(auto jt = (*it).begin(); jt != (*it).end(); ++jt){
  //     std::cout << (*jt) << ' ';
  //   }
  //   std::cout << std::endl;
  // }

  return 0;
}

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

static const int N = 100;
//static const int white = 0;
//static const int gray = 1;
//static const int black = 2;
static const int inf = (1 << 30);

int n, m[N][N];
int d[N];
//color[N];

void bfs(int s) {
	queue<int> q;
	for (int i = 0; i < n; i++) {
		//color[i] = white;
		d[i] = inf;
	}
	d[s] = 0;
	q.push(s);
	//color[s] = gray;
	while (!q.empty()) {
		for (int  i = 0; i < n; i++) {
			if (m[s][i] == 1 && d[i] == inf) {
				q.push(i);
				d[i] = d[s] + 1;
			}
		}
	//	color[s] = black;
		q.pop();
		s = q.front();
	}
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			m[i][j] = 0;
		}
	}	
	int u, k, v;
	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);
	for (int i = 0; i < n; i++) {
		if (d[i] == inf)
			cout << i + 1 << " " << -1 << endl;
		else
			cout << i + 1 << " " << d[i] << endl;
	}
	return 0;
}