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++: #include <iostream>
#include <vector>
#include <numeric>
#include <cassert>
#include <queue>

const int MAXINT = 1000000000;

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]){
      target_q.push(u);
      if(d[vtx] + 1 < d[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++: /* 
 * ALDS1_11B.cpp
 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int N;
int ad_k[101];
int ad_list[101][101];
int result[101];

struct Data {
    int d, v;
};

queue<Data> q;

int main(void) {

    scanf("%d", &N);
    for (int i = 1, id; i <= N; ++i) {
        scanf("%d", &id);
        scanf("%d", ad_k + id);
        for (int j = 0; j < ad_k[id]; ++j) {
            scanf("%d", ad_list[id] + j);
        }
    }

    q.push({0, 1});
    while (!q.empty()) {
        Data data = q.front();
        q.pop();

        for (int i = 0; i < ad_k[data.v]; ++i) {
            if (result[ad_list[data.v][i]] != 0 || ad_list[data.v][i] == 1)
                continue;
            result[ad_list[data.v][i]] = data.d + 1;
            q.push({data.d + 1, ad_list[data.v][i]});
        }
    }

    for (int i = 1; i <= N; ++i) {
        printf("%d ", i);
        if (result[i] == 0 && i != 1)
            printf("-1\n");
        else
            printf("%d\n", result[i]);
    }

    return 0;
}

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


std::queue<int> q;
typedef struct{
	bool isVisited;
	int degree;
	int distance;
	int *neighbors;
} node;

void bfs(node* nodes){
	int id;
	while (!q.empty()){
		id = q.front();
		q.pop();
		for (int i=0; i<nodes[id].degree; i++){
			if (!nodes[nodes[id].neighbors[i]].isVisited){
				nodes[nodes[id].neighbors[i]].distance = nodes[id].distance+1;
				q.push(nodes[id].neighbors[i]);
				nodes[nodes[id].neighbors[i]].isVisited=true;
			}
		}
	}
}

int main(){
	int n, d, r, id;
	node *nodes;

	scanf("%d", &n);
	nodes = new node[n+1];
	for (int i=0; i<n;i++){
		nodes[i].isVisited=false;
		scanf("%d %d", &r, &d);
		nodes[i].degree=d;
		nodes[i].neighbors = new int[d];
		nodes[i].distance=0;
		
		for (int j=0;j<d;j++){
			scanf("%d", &id);
			nodes[i].neighbors[j] = id-1;
		}
	}
	q.push(0);
	nodes[0].isVisited=true;
	bfs(nodes);
	for (int i=0; i<n;i++){
		printf("%d %d\n", i+1, nodes[i].distance);
		delete []nodes[i].neighbors;
	}
	delete []nodes;
	return 0;
}