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<queue>

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, G[100][100];
int d[100];

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(G[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(){
  int u, k, v;

  cin >> n;
  for(int i =0;i < n;i++){
    for(int j = 0;j < n;j++){
      G[i][j] = 0;
    }
  }

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

  bfs(0);

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <utility>
#include <set>
using namespace std;

int main(){
    int n;
    cin >> n;
    int g[110][110];
    int i, j;
    int u, k, v;
    int dist[110];
    bool visited[110];
    
    for(i = 0; i < n; i++){
        dist[i] = -1;
        for(j = 0; j < n; j++){
            g[i][j] = 0;
        }
    }
    
    for(i = 0; i < n; i++){
        cin >> u >> k;
        u--;
        for(j = 0; j < k; j++){
            cin >> v;
            v--;
            g[u][v] = 1;
        }
    }
    
    queue<int> q1, q2;
    q1.push(0);
    
    for(i = 0; q1.size(); i++){
        while(!q1.empty()){
            int p = q1.front();
            q1.pop();
            
            if(visited[p]) continue;
            visited[p] = true;
            
            dist[p] = i;
            
            for(j = 0; j < n; j++){
                if(g[p][j]){
                    q2.push(j);
                }
            }
        }
        q1 = q2;
        while(!q2.empty()){
            q2.pop();
        }
    }
    
    for(i = 0; i < n; i++){
        cout << i+1 << " " << dist[i] << endl;
    }
}

C++: #include <vector>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

int n, u, k, v, dp[101];
bool li[101][101];


int main(){
	fill(dp, dp + 101, -1);
	for (int i = 0; i < 101; i++) {
		fill(li[i], li[i] + 101, false);
	}
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			li[u][v] = true;
		}
	}
	vector<int> l = vector<int>(1, 1);
	vector<int> l2 = vector<int>();
	dp[1] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < l.size(); j++) {
			for (int k = 1; k <= n; k++) {
				if (li[l[j]][k] && dp[k] < 0) {
					dp[k] = i;
					l2.push_back(k);
				}
			}
		}
		l2.swap(l);
		vector<int>().swap(l2);
	}
	for (int i = 1; i <= n; i++) {
		cout << i << " " << dp[i] << endl;
	}

	return 0;
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
	int check_id;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
	check_id = 0;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == this->u) return 0;

	if (id == check_id) return -1;
	else check_id = id;

	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d + 1, d_min);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

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

const int white = 0, gray = 1, black = 2, N = 101, INF = -1;
bool G[N][N];
int n, color[N], d[N];

void init(){
    for (int i = 1; i <= n; i++)
    {
        color[i] = white;
        d[i] = INF;
    }
}

void bfs(int v){
    queue<int> q;
    q.push(v);
    d[v] = 0;
    int u;
    while (!q.empty())
    {
        u = q.front(); q.pop();
        for (int i = 1; i <= n; i++)
        {
            if (G[u][i] && color[i] == white)
            {
                d[i] = d[u] + 1;
                q.push(i);
                color[i] = gray;
            }
        }
    color[u] = black;
    }
}

int main(){
    int u, k, v;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> u >> k;
        for (int j = 1; j <= k; j++)
        {
            cin >> v;
            G[u][v] = 1;
        }
    }
    init();
    bfs(1);
    for (int i = 1; i <= n; i++)
    {
        cout << i << " " << d[i] << endl;
    }   
}

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

int main(){
	int n;
	cin >> n;

	int G[100][100]={0};

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

	int d[100];
	for(int i=0; i<n; i++){
		d[i] = -1;
	}
	queue<int> Q;

	Q.push(0);
	d[0] = 0;

	while(!Q.empty()){
		int u = Q.front();
		int v = -1;
		for(int j=0; j<n; j++){
			if(G[u][j]==1 && d[j]==-1){
				v = j;
				break;
			}
		}

		if(v!=-1){
			Q.push(v);
			d[v] = d[u]+1;
		}else{
			Q.pop();
		}
	}
	
	for(int i=0; i<n; i++){
		cout << i+1 << ' ' << d[i] << endl;
	}
	return 0;
}