Go: package main

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

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

int main()
{
    int G[101][102];
    int dist[101];

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, a, b, e, v1, v2;
    scanf("%d", &n);
    for(i = 1; i <= n; i++){
        for(j = 0; j <= n + 1; j++){ G[i][j] = 0; } }

    for(i = 1; i <= n; i++){
        scanf("%d %d", &a, &e);
		G[i][0] = e;  // number of edge.
        for(j = 1; j <= e; j++){
            scanf("%d", &b); G[a][j] = b; } }  // adjacent representation.

    for(i = 1; i <= n; i++) dist[i] = -1;
    f_insert(head, 1);
    dist[1] = 0;

    while(1){
        v1 = b_pop(tail);
		e = G[v1][0];
        for(i = 1; i <= e; i++){
            v2 = G[v1][i];
            if(dist[v2] < 0){
                dist[v2] = dist[v1] + 1;
                f_insert(head, v2);
            }
        }
        if(isEmpty(head, tail)) break;
    };

    for(i = 1; i <= n; i++){ printf("%d %d\n", i, dist[i]); }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

vector<int> g[101];
bool visited[101]{};
int n, ans[101]{};

void bfs() {
    fill(ans, ans + n + 1, -1);
    ans[1] = 0;
    queue<P> que;
    que.push({1, 0});
    visited[1] = true;
    while (!que.empty()) {
        int u, d;
        u = que.front().first;
        d = que.front().second;
        que.pop();
        REP(i, g[u].size()) {
            if (visited[g[u][i]]) continue;
            que.push({g[u][i], d + 1});
            visited[g[u][i]] = true;
            ans[g[u][i]] = d + 1;
        }
    }
}

int main() {
    cin >> n;
    REP(i, n) {
        int u, m;
        cin >> u >> m;
        REP(j, m) {
            int v;
            cin >> v;
            g[u].push_back(v);
        }
    }
    bfs();
    for(int i=1;i<=n;i++){
        cout << i << " " << ans[i] << endl;
    }
    return 0;
}

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

using namespace std;

const int INF = 100000000;

int main(){
	int n;
	cin >> n;
	vector<vector<int>> node(n+1);
	for(int i=0;i<n;i++){
		int u,k,tmp;
		cin >> u >> k;
		for(int j=0;j<k;j++){
			cin >> tmp;
			node[u].push_back(tmp);
		}
	}
	
	//search
	vector<int> res(n+1,INF);
	queue<int> que;
	queue<int> copied;
	queue<int> zeros;
	que.push(1);
	
	int count = 0;
	for(int i=0;i<n;i++){
		while(que.size()){
			int k = que.front(); que.pop();
			//cout << "-> " << k << endl;
			if(res[k] >= INF) res[k] = count;
			for(int v:node[k]){
				copied.push(v);
			}
		}
		count++;
		que = copied;
		copied = zeros;
	}

	//output
	for(int i=1;i<=n;i++){
		if(res[i] >= INF) res[i] = -1;
		cout << i << " " << res[i] << endl;
	}

	return 0;
}

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

const int MAX = 100;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int N;
int M[MAX][MAX];

int d[MAX];

queue<int> Q;

void bfs() {
    for (int i = 0; i < N; i++) {
        d[i] = -1;
    }
    
    Q.push(0);
    d[0] = 0;

    while (!Q.empty()) {
        int u = Q.front(); Q.pop();

        for (int i = 0; i < N; i++) {
            if (M[u][i] == 1 && d[i] == -1) {
                Q.push(i);
                d[i] = d[u] + 1;
            }
        }
    }
}

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
void func(vector<vector<int> > &G, int *depth)
{
    queue<int> q;
    int p = 1;
    q.push(p);
    depth[p] = 0;
    while (q.empty() == false)
    {
        p = q.front();
        q.pop();
        rep(i, 0, G[p].size())
        {
            if (depth[G[p][i]] == -1)
            {
                q.push(G[p][i]);
                depth[G[p][i]] = depth[p] + 1;
            }
        }
    }
}
int main()
{
    int n;
    cin >> n;
    int ans[n + 1], u, k, v;
    vector<vector<int> > G(n + 1, vector<int>(0));
    rep(i, 0, n)
    {
        ans[i + 1] = -1;
        cin >> u >> k;
        rep(j, 0, k)
        {
            cin >> v;
            G[u].push_back(v);
        }
    }
    func(G, ans);
    rep(i, 1, n + 1)
    {
        printf("%d %d\n", i, ans[i]);
    }
}