Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

#define vi vector<int>
#define vl vector<long long>
#define vvi vector< vector<int> >
#define vvl vector< vector<ll> >
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define INF 1e18
#define MOD 1e9+7

template<class T> bool chmax(T &a, const T & b) {
    if (a<b) {
        a = b;
        return true;
    }
    return false;
}

template<class T> bool chmin(T &a, const T & b) {
    if (a>b) {
        a = b;
        return true;
    }
    return false;
}

const int MAX_N = 110;
vector < vector<int> > G;
int d[MAX_N];

void bfs(int s) {
    d[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        REP(i, (int)G[u].size()) {
            int v = G[u][i];
            if (d[v] != -1) continue;
            d[v] = d[u]+1;
            q.push(v);
        }
    }
}

int main() {
    int n;
    cin >> n;
    G.resize(n);
    REP(i, n) {
        d[i] = -1;
        int u, k;
        cin >> u >> k;
        --u;
        REP(j, k) {
            int v;
            cin >> v;
            --v;
            G[u].push_back(v);
        }
    }

    bfs(0);
    REP(i, n) {
        cout << i+1 << ' ' << d[i] << endl;
    }
}

C++: #include <cstdio>

#define N 100

int check(int id, int distance, int a[N + 1][N], int count[N], int d[N]){
    if(d[id] != -1 && distance >= d[id]) return d[id];
    d[id] = distance;
    for(int i = 0; i < count[id]; ++i){
        check(a[id][i], distance + 1, a, count, d);
    }
    return d[id];
}

int main(){
    int n;
    std::scanf("%d", &n);

    int a[N + 1][N];
    int d[N + 1];
    int count[N + 1];
    for(int i = 1; i <= n; ++i){
        int id;
        std::scanf("%d", &id);
        std::scanf("%d", &count[id]);
        for(int j = 0; j < count[id]; ++j){
            std::scanf("%d", &a[id][j]);
        }
        d[id] = -1;
    }

    check(1, 0, a, count, d);

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

    return 0;
}

C++: /*
 * ALDS1_11_C.cpp
 *
 *  Created on: May 8, 2018
 *      Author: 13743
 */

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

using namespace std;

queue<int> Q;
int d[101];

void bfs(vector<int>* a, int rt) {
	Q.push(rt);
	d[rt] = 1;
	while(!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for(int &x:a[u]) {
			if(d[x] == 0) {
				Q.push(x);
				d[x] = d[u] + 1;
			}
		}

	}
}

int main() {
	int n;
	vector<int> adj[101];

	cin >> n;
	for(int i=0; i<n; i++) {
		int u, k;
		cin >> u >> k;
		while(k--) {
			int v;
			cin >> v;
			adj[u].push_back(v);
		}
	}

	bfs(adj, 1);

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

}

C++: //さっさと済ませられるところはさっさと済ましてしまう．ただし注意深く
#include <cstdio>
#include <iostream>
#include <queue>
#define MAX_VERTEXES 100
#define ZERO 0
#define NIL -1

using namespace std;

//think easy make easy

int n;//num of vertexes(頂点)
int matrix_graph[MAX_VERTEXES+1+1][MAX_VERTEXES+1+1];//1オリジン
int min_path[MAX_VERTEXES+1+1];


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

	int v_num;
	int k;//v_numの出次数

	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			matrix_graph[i][j] = ZERO;
		}
	}

	for(int i=0;i<n;i++){
		scanf("%d",&v_num);
		scanf("%d",&k);

		for(int j=0;j<k;j++){
			int v_num2;
			scanf("%d",&v_num2);
			matrix_graph[v_num][v_num2] = 1;
		}
	}

	for(int i=1;i<=n;i++){
		min_path[i] = NIL;
	}
	min_path[1] = 0;

	int s = 1;
	queue<int> q;
	q.push(1);
	while(!q.empty()){
		s = q.front();
		q.pop();
		for(int i=1;i<=n;i++){
			if(min_path[i] == NIL && matrix_graph[s][i] == 1){
				q.push(i);
				min_path[i] = min_path[s] + 1;
			}
		}
	}
	for(int i=1;i<=n;i++){
		cout <<i <<" " <<min_path[i] <<endl;
	}

	return 0;
}

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

using namespace std;

const int N = 100;


int main() {
    int A[N][N] = {{0}};

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int u;
        cin >> u;

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

    vector<int> d(n, -1);
    using int_pair = pair<int, int>;
    queue<int_pair> Q;
    Q.push(int_pair(0, 0));
    while (!Q.empty()) {
        auto p = Q.front();
        Q.pop();

        int k = p.first;
        int t = p.second;
        if (d[k] == -1) {
            d[k] = t;
        } else if (d[k] < t) {
            continue;
        } else {
            d[k] = t;
        }
        for (int i = 0; i < n; ++i) {
            if (A[k][i] != 0) {
                Q.push(int_pair(i, t + 1));
            }
        }
    }

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

    return 0;
}