Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <cstdlib>
#include <climits>

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 = INT_MAX;
    node->visited = false;
    
    nodes->at(v) = 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();

      if (nodes->at(v)->d < count) {
	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;

    if (node->d == INT_MAX) {
      node->d = -1;
    }
        
    std::cout << node->v << " " << node->d << std::endl;
  }
  
  return 0;
}

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++: #include<iostream>
#include<cstdio>
#define INF 1000000
#define NIL -2
#define GRAY -3
#define WHITE -4
#define BRACK -5

using namespace std;

int n;
int M[101][101];
int d[101];//重み
//int p[101];//親
int color[101];

void Dijkstra(int u) {
	//cout << u << endl;
	color[u] = BRACK;

	//重みの更新
	for (int i = 1; i <= n; i++) {
		if (color[i] != BRACK && M[u][i] != 0) {
			color[i] = GRAY;
			if ((d[u] + M[u][i]) < d[i]) {
				d[i] = d[u] + M[u][i];
			}
		}
		//cout << d[i] << " ";
	}

	//cout << endl;

	int min = -1;
	d[min] = 100000;


	for (int v = 1; v <= n; v++) {
		if (color[v] != BRACK) {
			if (d[v] < d[min]) {
				min = v;
			}
			else {
				color[v] = WHITE;
			}
		}
	}

	if (min != -1) {
		Dijkstra(min);
	}
}

int main() {
	cin >> n;
	int v, k, g, weight; //v = 節点番号　g = 次数

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			M[i][j] = 0;
		}
	}

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

	for (int i = 1; i <= n; i++) {
		color[i] = WHITE;
	}

	for (int i = 1; i <= n; i++) {
		d[i] = INF;
		//cout << d[i] << " ";
	}

	d[1] = 0;


	//関数
	Dijkstra(1);


	for (int i = 1; i <= n; i++) {
		if (d[i] == INF) {
			d[i] = -1;
		}
		cout << i << " " << d[i] << endl;
	}

	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] == 0)continue;
			if(d[v] != INF)continue;
			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 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<iostream>
#include<queue>
using namespace std;
const int MAX = 100;
int n, G[MAX + 1][MAX + 1];
bool book[MAX + 1] = { false };
int path[MAX + 1];
void breathFSearch(int orign)
{
	queue<int> Q;
	Q.push(orign);
	book[orign] = true;
	while (!Q.empty())
	{
		int u = Q.front();
		for (int v = 1; v <= n; v++)
		{
			if (G[u][v] && !book[v])
			{
				Q.push(v);
				book[v] = true;
				if(path[u]!=-1)
					path[v] = path[u]+1;
			}
		}
		Q.pop();
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int u, d;
		cin >> u >> d;
		for (int j = 1; j <= d; j++)
		{
			int v;
			cin >> v;
			G[u][v] = 1;
		}
	}
	for (int i = 1; i <= n; i++)
		path[i] = -1;
	path[1] = 0;
	for(int i=1;i<=n;i++)
		if(!book[i])
			breathFSearch(i);
	for (int i = 1; i <= n; i++)
		cout << i << " " << path[i] << endl;
}