Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

using namespace std;

int n, t;
vector<int> G[101];
int d[101];

void bfs(int v) {
	d[v] = 0;
	queue<int> q;
	q.push(v);
	while (!q.empty()) {
		int e = q.front(); q.pop();
		for (int i = 0; i < G[e].size(); ++i) {
			int ne = G[e][i];
			if (d[ne] != -1) continue;
			d[ne] = d[e] + 1;
			q.push(ne);
		}
	}
}

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

	memset(d, -1, sizeof(d));
	bfs(1);

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

	return 0;
}

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

      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<bits/stdc++.h>

using namespace std;

int main(void){
    int n;
    cin >> n;
    vector<vector<int> > a(n+1);
    for(int i = 1; i <= n; i++){
        int id, size;
        cin >> id >> size;
        a[i].resize(size);
        for(int j = 0; j < size; j++){
            int temp;
            cin >> temp;
            a[i][j] = temp;
        }
    }
    vector<int> d(n+1);
    vector<bool> ok(n+1);
    queue<int> s;
    s.push(1);
    ok[1] = true;
    while(!s.empty()){
        int now = s.front();
        s.pop();
        for(int i = 0; i < a[now].size(); i++){
            int next = a[now][i];
            if(!ok[next]){
                d[next] = d[now] + 1;
                s.push(next);
                ok[next] = true;
            }
        }
    }
    for(int i = 2; i <= n; i++){
        if(d[i] == 0)d[i] = -1;
    }
    for(int i = 1; i <= n; i++){
        cout << i << " " << d[i] << endl;
    }
    return 0;
}

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

int time, n;
int d[100], color[100];
int M[100][100];

void bfs(int s) {
    queue<int> Q;
    
    for (int i=0; i<100; i++) color[i] = 0:
    for (int i=0; i<100; i++) d[i] = -1;
    
    color[s] = 1;
    d[s] = 0;
    Q.push(s);
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        for (int i=0; i<n; i++) {
            if(M[u][i] && color[i] == 0) {
                color[i] = 1;
                d[i] = d[u]+1;
                Q.push(i);
            }
        }
        color[u] = 2;
    }
}

int main() {
    int u, k, v;
    scanf("%d", &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++) {
        scanf("%d %d", &u, &k);
        for (int j=0; j<k; j++) {
            scanf("%d", &v);
            M[u-1][v-1] = 1;
        }
    }
    
    bfs(0);
    
    for (int i=0; i<n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
    return 0;
}

C++: 

#include <iostream>
#include<queue>
#include<string>
#define NIL -1
using namespace std;
string color[101];//白色未访问，灰色访问，黑色退出队列
queue<int >BreathFirstSearch;//创建队列
int Distance[101];
int AM[101][101];
int total;
void includeInsertQueue(int i)
{
	for (int j = 0; j <= total; j++)
	{
		if (AM[i][j] && color[j] == "White")//存在且未被访问
		{
			color[j] = "Grey", Distance[j] =Distance[i]+1;//找到的点距离是其上一个再加一
			BreathFirstSearch.push(j);
		}
	}
}
void includeFirstSearch(int i)
{
	int next;
	BreathFirstSearch.push(i);//压入起点
	Distance[i] =0;//起点到起点的距离为0；
	color[i] = "Grey";
	while (!BreathFirstSearch.empty())
	{
		next = BreathFirstSearch.front();//队列头中取出元素
		BreathFirstSearch.pop();
		color[next] = "Black";
		includeInsertQueue(next);

	}
}
int main()
{	int TotalOfConnectedNode, node[101], connectednode;
	for (int i = 0; i < 101; i++) Distance[i]= NIL;
	for (int i = 0; i < 101; i++)color[i] = "White";
	for (int i = 0; i < 101; i++)
		for (int j = 0; j < 101; j++)
			AM[i][j] = 0;
	cin >> total;
	for (int i = 0; i < total; i++)//有向图
	{
		cin >> node[i] >> TotalOfConnectedNode;
		if (TotalOfConnectedNode == 0) continue;
		for (int j = 0; j < TotalOfConnectedNode; j++)
		{
			cin >> connectednode;
			AM[node[i]][connectednode] = 1;
		}
	}
	includeFirstSearch(node[0]);
	for (int i = 0; i < total; i++)
		cout << node[i] << " " << Distance[node[i]] << endl;

	
	return 0;
}