Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

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

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

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

enum Status {WHITE, GRAY, BLACK};

queue<int> que;
Status *status;
int *dist;
vector<vector<int> > a;
int n;

int main(){
        cin>>n;
        a = vector<vector<int> >(n, vector<int>());
        status = new Status[n];
        dist = new int[n];

        int m, k, r;
        for(int i=0;i<n;i++){
                cin>>r;
                cin>>m;
                for(int j=0;j<m;j++){
                        cin>>k;
                        a[r-1].emplace_back(k-1);
                }
        }
        for(int i=0;i<n;i++) status[i] = WHITE;
        for(int i=0;i<n;i++) dist[i] = -1;

        que.push(0);
        dist[0] = 0;
        while(!que.empty()){
                int v = que.front(); que.pop();
                if(status[v]==BLACK) continue;
                for(int i=0;i<a[v].size();i++){
                        if(status[a[v][i]]==WHITE){
                                dist[a[v][i]] = dist[v]+1;
                                status[a[v][i]] = GRAY;
                                que.push(a[v][i]);
                        }
                }
                status[v] = BLACK;
        }

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

C++: //#define LOCAL

#include<bits/stdc++.h>

using namespace std;

#define rep(a, b) for(int i = a; i <= b; i++)
#define mst(a) memset(a, 0, sizeof(a))

const int MAX = 1e2 + 10;


bool mtx[MAX][MAX];
int vis[MAX];
int n;

queue<int> q;

void init(void)
{
	while(!q.empty())
		q.pop();
	mst(vis);
	mst(mtx);
	n = 0;
	return;
}

bool check(int x, int y)
{
	if(mtx[x][y] && !vis[y])
		return true;
	return false;
}

void GetAns(void)
{
	rep(2, n)
	{
		if(vis[i] == 0)
			vis[i] = -1;
	}
	return;
}

void Bfs(void)
{
	q.push(1);
	while(!q.empty())
	{
		int cur = q.front();
		q.pop();
		rep(1, MAX - 10)
		{
			if(check(cur, i))
			{
				q.push(i);
				vis[i] = vis[cur] + 1;
			}
		}
	}
	return;
}

int main()
{
/*#ifdef LOCAL
	freopen("in.txt", "r", stdin);
#endif*/

	while(~scanf("%d", &n))
	{
		rep(1, n)
		{
			int row;
			scanf("%d", &row);
			int num;
			scanf("%d", &num);
			for(int j = 1; j <= num; j++)
			{
				int tmp;
				scanf("%d", &tmp);
				mtx[row][tmp] = true;
			}
		}
		
		Bfs();

		GetAns();
		
		rep(1, n)
			printf("%d %d\n", i, vis[i]);
	}
	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);
    int i, u, v;
    for ( i = 0; i < n; i++ ) d[i] = INFTY;
    d[s] = 0;
 
    while ( !q.empty() ) {
        u = q.front();
        q.pop();
        for ( 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 ( i = 0; i < n; i++ ) {
            cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
    }
}
 
int main() {
    int u, k, v, i, j;
 
    cin >> n;
 
    for ( i = 0; i < n; i++ ) {
        for ( j = 0; j < n; j++ ) M[i][j] = 0;
    }
 
    for ( i = 0; i < n; i++ ) {
        cin >> u >> k;
        u--;
        for ( j = 0; j < k; j++ ) {
            cin >> v;
            v--;
            M[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;
        q2 = queue<int>();
    }
    
    for(i = 0; i < n; i++){
        cout << i+1 << " " << dist[i] << endl;
    }
}