Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

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() {
    int u, k,v;
    
    cin >> n;
    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 <bits/stdc++.h>
using namespace std;
#define MAX 105

vector<vector<int> > vec;
vector<int> ve;
int dis[MAX];
queue<int> Q;
int n;

int search(int start, int end) {
	while(!Q.empty()) Q.pop();
	Q.push(start);
	for (int i = 1; i <= n; i++) {
		dis[i] = 0;
	}
	int now, len = 0;
	while (!Q.empty()) {
		now = Q.front();
		Q.pop();
		if (now == end)
			return dis[now];
		if (dis[now] == len)
			len++;
		for (int cnt = 1; cnt <= vec[now][0]; cnt++) {
			if (dis[vec[now][cnt]] == 0) {
				dis[vec[now][cnt]] = len;
				Q.push(vec[now][cnt]);
			}
		}
	}
	return -1;
}

int main(void) {
	vec.push_back(ve);
	int u, k, v, i;
	cin >> n;
	for (i = 1; i <= n; i++) {
		ve.clear();
		cin >> u >> k;
		ve.push_back(k);
		while (k--) {
			cin >> v;
			ve.push_back(v);
		}
		vec.push_back(ve);
	}

	for (i = 1; i <= n; i++) {
		cout << i << " " << search(1, i) << endl;
	}
	
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N], 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() {
	int v, k ,u;
	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;
		for(int j = 0; j < k; j++) {
			cin >> v;
			M[u-1][v-1] = 1;
		}
	}
	
	bfs(0);
	
	return 0;
	
}

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

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(q->size() > 0)
    {
        int u = q->front();
        q->pop();

        for (int i = 0; i < N; i++)
        {
            if (M[u][i] && C[i] == WHITE)
            {
                C[u] = GRAY;
                D[i] = D[u] + 1;
                q->push(i);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

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

    for (int i = 0; i < N; i++)
    {
        int u, k;
        cin >> u >> k;
        u--;

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

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }

    bfs(0);

    for (int i = 0; i < N; i++)
    {
        cout << D[i] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef vector<int> Adj_list;
typedef vector<Adj_list> Adj_lists;

void bfs(const int& i, const Adj_lists& adj_lists, vector<int>& distances){

  distances.at(i-1) = 0;

  queue<int> q;
  q.push(i);

  while(!q.empty()){
    // visit top of queue
    const int& visit = q.front();
    const Adj_list& adj_list = adj_lists.at(visit-1);

    // push adjecent node which is not visited yet
    for(const int& adj : adj_list){
      if(distances.at(adj-1) == -1){
        distances.at(adj-1) = distances.at(visit-1) + 1;
        q.push(adj);
      }
    }

    q.pop();
  }
}

int main(){

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

  Adj_lists adj_lists(n);
  for(int i=0; i<n; i++){
    int u, k;
    scanf("%d %d", &u, &k);
    Adj_list adj_list(k);
    for(int j=0; j<k; j++){
      int v;
      scanf("%d", &v);
      adj_list[j] = v;
    }
    sort(adj_list.begin(), adj_list.end());
    adj_lists[u-1] = adj_list;
  }

  vector<int> distances(n);
  for(int& distance : distances) distance = -1;

  bfs(1, adj_lists, distances);

  for(int i = 0; i < distances.size(); i++){
    printf("%d %d\n", i+1, distances.at(i));
  }

  return 0;
}