Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[101][101];
int d[101];
int searchPos[101];
int color[101];
int n;
queue<int> q;

/*int nextPoint(int curPoint){
    for(int i=searchPos[curPoint];i<n;++i){
        searchPos[curPoint] += 1;
        if(M[curPoint][i]==1 && color[i] == WHITE){
            return i;
        }
    }
    return -1;
}*/

void bfs(int point){
    q.push(point);
    d[point] = 0;
    color[point] = GRAY;
    int cur, next;
    while(q.size()>0){
        cur = q.front();
        q.pop();
        color[cur] = BLACK;
        for(int i=0;i<n;++i){
            if(M[cur][i] != 0 && color[i] == WHITE){
                q.push(i);
                color[i] = GRAY;
                d[i] = d[cur]+1;
            }
        }
    }
}

int main(){
    int u, z, y;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        searchPos[i] = 0;
        color[i] = WHITE;
        d[i] = 0;
        for(int j=0;j<n;++j){
            M[i][j] = 0;
        }
    }
    for(int i=0;i<n;++i){
        cin >> u >> z;
        for(int j=0;j<z;++j){
            cin >> y;
            M[u-1][y-1] = 1;
        }
    }

    bfs(0);
    for(int i=0;i<n;++i){
        if(color[i]==WHITE){
            // can't reach
            d[i] = -1;
        }
    }
    for(int i=0;i<n;++i){
        printf("%d %d\n",i+1,d[i]);
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int Max = 100;
const int white = 0;
const int black = 1;
int M[Max][Max];
int d[Max], nt[Max], color[Max];
int n;
queue<int> Q;

int next(int u){
    for(int v=nt[u]; v<n; v++){
        nt[u] = v + 1;
        if(M[u][v]) return v;
    }
    return -1;
}

void bfs_visit(int u){
    for(int i=0; i<n; i++){
        nt[i] = 0;
        d[i] = -1;
    }

    color[u] = black;
    d[u] = 0;
    Q.push(u);
    while(!Q.empty()){
        int u = Q.front();
        int v = next(u);
        if(v != -1){
            if(color[v] == white){
                color[v] = black;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        } else {
            Q.pop();
        }
    }
}

int main(){
    int u, k, v;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        color[i] = white;
    }
    for(int i=0; i<n; i++){
        cin >> u >> k;
        u--;
        for(int i=0; i<k; i++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs_visit(0);

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

C++: #include<stdio.h>

int main()
{
  int n, h, i, j, k, l, m, z, flag, Q[100], Z[100];
  int G[100][100];
  scanf("%d", &n);
  
  for(i = 0; i < 100; i++ ) Z[i] = -1;
  
  for( i = 0; i < n; i++){
    scanf("%d", &G[i][0]);
    scanf("%d", &G[i][1]);
    for( j = 2; j < 2 + G[i][1]; j++){
      scanf("%d", &G[i][j]);
    }
  }
  
  m = 0; 
  h = 0; 
  Q[h] = 1;
  z = 0;
  Z[1] = z;
  z++;
  for( i = 0; i < n; i++){
    flag = 0;
    k = Q[m];
    l = G[k - 1][1];
    
    for( j = 0; j < l; j++){
      if( Z[ G[k - 1][2 + j] ] == -1 ){
        h++;
        Q[h] = G[k - 1][2 + j];
        Z[ G[k - 1][2 + j] ] = z;
        flag = 1;
      }
    }
    if( flag == 1 ) z++;
    m++;
    if( Q[m] == 0 ) i = i + n;
  }
  
  for(i = 1; i <= n ; i++){
    printf("%d %d\n", i, Z[i] );
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <queue>
#define NUM 100
#define NIL -1
using namespace std;

int V[NUM + 1][NUM + 1];
int dist[NUM + 1];
int node_num;
int t;
int pos;
queue<int> Q;

void init(){
    int V[NUM + 1][NUM + 1];
    for(int i = 1; i <= NUM; i++){
        for(int j = 1; j <= NUM; j++){
            V[i][j] = 0;
        }
    }

    for(int i = 0; i <= NUM; i++) {
        dist[i] = NIL;
    }
    
    t = 0;
}

void get_adj(){
    scanf("%d", &node_num);

    for(int i = 0; i < node_num; i++){
        int num1;
        scanf("%d", &num1);

        int n;
        scanf("%d", &n);

        int num2;
        for(int j = 0; j < n; j++){
            scanf("%d", &num2);
            V[num1][num2] = 1;
        }
    }
}

void touchNextNode(int n){
    printf("before: %d\n", n);
    t += 1;
    for(int i = 1; i <= node_num; i++){
        if(V[n][i] == 1 && dist[i] == NIL){
            printf("i %d\n", i);
            Q.push(i);
            dist[i] = dist[n] + 1;
        }
    }
    Q.pop();
}

void BFS(){
    Q.push(1);
    dist[1] = t;
    touchNextNode(1);
    
    while(Q.size()){
        int pos = Q.front();
        touchNextNode(pos);
    }
}

int main(void){
    init();
    get_adj();
    BFS();
    
    for(int i = 1; i <= node_num; i++){
        printf("%d %d\n", i, dist[i]);
    }

}

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