Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<cstdio>
#include<queue>
using namespace std;
#define INFTY -1
#define MAX 100

void InitializeBoolMatrix(int nmbr_of_rows, int nmbr_of_columns, bool mtrx[][MAX]) {
	for (int row = 0; row < nmbr_of_rows; ++row) {
		for (int column = 0; column < nmbr_of_columns; ++column)
			mtrx[row][column] = false;
	}
}

void ListToMatrix(int number_of_vertices, bool mtrx[][MAX]) {
	int vertex_from, vertex_to, degree_out;	
	for (int vertex_i = 0; vertex_i < number_of_vertices; ++vertex_i) {
		scanf("%d %d", &vertex_from, &degree_out);
		vertex_from--; //0?????????????????????
		for (int vertex_out_i = 0; vertex_out_i < degree_out; ++vertex_out_i) {
			scanf("%d", &vertex_to);
			vertex_to--; //0?????????????????????
			mtrx[vertex_from][vertex_to] = true;
		}
	}
}

void BuildAdjacencyMatrix(int n, bool adjmtrx[][MAX]) {
	InitializeBoolMatrix(n, n, adjmtrx);
	ListToMatrix(n, adjmtrx);
}

void Bfs(int n, int depth[], bool adjmtrx[][MAX]) {
	bool yet_to_be_visited[n];
	for (int i = 0; i < n; ++i) {
		yet_to_be_visited[i] = true;
		depth[i] = INFTY;
	}

	depth[0] = 0;
	queue<int> next_vertex_to_visit;
	next_vertex_to_visit.push(0);

	while( !next_vertex_to_visit.empty() ) {
		int u = next_vertex_to_visit.front();
		next_vertex_to_visit.pop();
		for (int v = 0; v < n; ++v)	{
			if( adjmtrx[u][v] && yet_to_be_visited[v]){
				yet_to_be_visited[v] = false;
				depth[v] = depth[u] + 1;
				next_vertex_to_visit.push(v);
			}
		}
		yet_to_be_visited[u] = false;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	bool adjmtrx[MAX][MAX];
	BuildAdjacencyMatrix(n, adjmtrx);
	int depth[n];
	Bfs(n, depth, adjmtrx);
	for (int id = 0; id < n; ++id)
		printf("%d %d\n", id + 1, depth[id]);
	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(){
    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++: //
// Created by tattsun on 2017/02/14.
//

#include <iostream>
#include <vector>
#include <queue>

#define VERTEX_NUM_MAX 101

using namespace std;
typedef vector<int> Vertex;

int n;
Vertex vs[VERTEX_NUM_MAX];
int ds[VERTEX_NUM_MAX];

/*
void _bfs(vector<int>& targets, int distance) {
    vector<int> next_targets;

    for(auto it = targets.begin(); it != targets.end(); it++) {
        if(ds[*it] == -1) {
            ds[*it] = distance;

            auto& t = vs[*it];
            for(auto itt = t.begin(); itt != t.end(); itt++)
                next_targets.push_back(*itt);
        }
    }

    if(next_targets.empty())
        return;

    _bfs(next_targets, distance+1);
}

void bfs() {
    vector<int> targets;
    targets.push_back(1);

    _bfs(targets, 0);
}
 */

void bfs() {
    queue<int> q;
    q.push(1);
    ds[1] = 0;

    while(!q.empty()) {
        int i = q.front();
        q.pop();

        auto& v = vs[i];
        for(auto it = v.begin(); it != v.end(); it++) {
            if(ds[*it] != -1)
                continue;

            ds[*it] = ds[i] + 1;
            q.push(*it);
        }
    }
}

int main() {
    for(int i=0; i<VERTEX_NUM_MAX; i++) {
        ds[i] = -1;
    }

    cin >> n;

    int u, k, p;
    for(int i=1; i<=n; i++) {
        cin >> u >> k;
        for(int j=0; j<k; j++) {
            cin >> p;
            vs[i].push_back(p);
        }
    }

    bfs();

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

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;
#define N 100
#define INF 1e9

queue<int> Q;
int n,M[N][N];
int dist[N];

void bsf(int l){
	Q.push(l);
	for(int i = 0;i < n;i++) dist[i] = INF;

	dist[l]=0;
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		for(int i=0;i<n;i++){
			if(M[u][i] == 0)continue;
			if(dist[i] != INF)continue;
			dist[i] = dist[u] + 1;
			Q.push(i);
		}
	}

	for(int i=0;i<n;i++){ 
		if(dist[i] == INF)dist[i] = -1;

		cout<<i+1<<" "<<dist[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;
		}
	}
	bsf(0);
	return 0;
}

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

static int INFTY = (1<<21);
int n, M[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(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++) {
        if (d[i] == INFTY) d[i] = -1;
        cout << i + 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;
}