Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include <queue>
#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

int map_max;
int map[110][110];
int rows;
int cols[110];
int howfar[110];
int far;
int visited[110];

void get_map();
void print_map();
int search(int node);
void print_result();
void check_result();

int main()
{

  get_map();
  //print_map();
  for(int i=0;i<map_max;i++)
    {
      visited[i]=1000;
    }
  search(1);
  check_result();
  print_result();
  return 0;
}

void check_result()
{
  for(int i=1;i<map_max;i++)
    {
      if(visited[i]==1000)
	{
	  visited[i]=-1;
	}
    }
}

void print_result()
{
  for(int i=0;i<map_max;i++)
    {
      std::cout << i+1 << " " << visited[i] << std::endl;
    }
}

void get_map()
{

  int k;
  scanf("%d", &map_max);//<100

  for(int i=0;i<map_max;i++)
    {
      scanf("%d", &k);
      scanf("%d", &cols[i]);
      for(int j=0;j<cols[i];j++)
	{
	  map[i][j]=0;
	  scanf("%d", &map[i][j]);
	}
    }

  
  return ;
}

void print_map()
{
  for(int i=0;i<map_max;i++)
    {
      for(int j=0;j<cols[i];j++)
	{
	  printf("%d", map[i][j]);
	}
      puts("");
    }
  return ;
}


int search(int node)
{
  far =1;
  int next =1, now,buf;
  visited[node-1]=0;
  
  queue<int> q;//push root node
  for(int i=0;i<cols[node-1];i++)
    {
      now = map[node-1][i];
      q.push(now);//
      visited[now-1]=visited[node-1]+1;
      
    }

  while(!q.empty())
  {
    next = q.front();
    q.pop();
    for(int i=0;i<cols[next-1];i++)
      {
	now = map[next-1][i];
	if(visited[now-1]==1000)
	  {
	    q.push(now);
	    visited[now-1]=min(visited[next-1]+1, visited[now-1]);
	  }
      }
  }

  return 0;
}

/*
  int search(int node, int far)
  {
  int now = node-1;
  if(visited[now] == 1)//searched
  {
  if(howfar[now]>far)
  {
  howfar[now]=far;
  }
  //return -1;
  }
  else
  {
  howfar[now]=far;
  visited[now]=1;

  //for(int i=0;i<cols[node-1];i++)
  //{
  //  //if(map[node-1][i]!=0)
  //  {
  //  search(map[node-1][i], far+1);
  //  }
  //}
  //return -1;
  }

  for(int i=0;i<cols[now];i++)
  {
  //if(map[now][i]!=0)
  {
  search(map[now][i], far+1);
  }
  }
    

  return -1;
  }
*/

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

using namespace std;

int main(){
	int n, u, k, v;
	int D[100];
	bool adj[100][100] = {};
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> u >> k;
		for (int j = 0; j < k; j++){
			cin >> v;
			adj[u-1][v-1] = true;
		}
	}
	for (int i = 0; i < n; i++){
		D[i] = -1;
	}
	queue<int> Q;
	Q.push(1);
	D[0] = 0;
	e[0] = 1;
	while(!Q.empty()){
		int m = Q.front();
		Q.pop();
		for (int i = 0; i < n; i++){
			if (adj[m-1][i] == 1 && e[i] == 0){
				D[i] = D[m-1]+1;
				Q.push(i);
				e[i] = 1;
			}
		}
	}
	
	for (int i = 0; i < n; i++){
		cout << i+1 << ' ' << D[i] << endl;
	}
}

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


using namespace std;


static const  int N =  100;
static const  int no =  -2;

int graph[N][N] = {{}};
int d[N] = {};
int n;

void bfs(int v){
  queue<int> Q; 
  Q.push(v);

  int u;
    for(int i = 1;i <= n; i++){
      d[i] = no;
    
}
  d[v] = 0;

  while(!Q.empty()){
    u = Q.front();
    Q.pop();

    for(int i = 1;i <= n; i++){
      if(graph[u][i] == 0) continue;
      if(d[i] != no) continue;
      d[i] = d[u] +1;
      Q.push(i);
    }
  }
}


int main(){
  int u,v,k;
  //graph
  cin >> n ;

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


  bfs(1);

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

  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() {

  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--; // to 0 origin
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--; // to 0 origin
      M[u][v] = 1;
    }
  }

  bfs(0);

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main()
{
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<vector<int> > mat(n + 5, vector<int>(n + 5, 0));
  for (int i = 0; i < n; ++i) {
    int v = -1, k = -1, u = -1;
    fscanf(stdin, "%d %d", &v, &k);
    for (int j = 0; j < k; ++j) {
      fscanf(stdin, "%d", &u);
      mat[v][u] = 1;
    }
  }
  vector<int> visited(n + 5, 0), cost(n + 5, INF);
  queue<int> que;
  cost[1] = 0;
  que.push(1);
  while (!que.empty()) {
    int v = que.front(); que.pop();
    if (visited[v]) { continue; }
    visited[v] = 1;
    for (int u = 1; u <= n; ++u) {
      if (mat[v][u] && !visited[u]) {
        cost[u] = min(cost[u], cost[v] + 1);
        que.push(u);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    fprintf(stdout, "%d %d\n", i, visited[i] ? cost[i] : -1);
  }
  return 0;
}

C++: #include<iostream>
#include<cstring>
#include<queue>

using namespace std;
#define MAX 100

const int INF = (1<<21);
int mat[MAX][MAX] = {};
int n, d[MAX] = {};
bool c[MAX];

void bfs(int u){
    queue<int> p; 
    int now = 0;
    d[u] = now++;
    c[u] = true;

    p.push(u);
    while(!p.empty()){
        int x = p.front(); p.pop();
        for(int i = 0; i < n; i++){
            if(mat[x][i] && !c[i]){
                p.push(i);
                c[i] = true;
                d[i] = d[x]+1;
            }
        }
    }
}

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i++){
        int x, k, y;
        cin >> x >> k;
        while(k-- > 0){
            cin >> y;
            mat[x-1][y-1] = 1;
        }
    }

    memset(d, INF, MAX);
    bfs(0);

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