Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

C++: #include <iostream>
#include <queue>
#include <assert.h>
#define INF 100000000
#define MAX_V 100
using namespace std;

int adjacencyMatrix[MAX_V][MAX_V] = {};
int d[MAX_V];
int V;

void bfs(int start)
{
    queue<int> Q;
    Q.push(start);

    for (int i = 0; i < V; ++i)
        d[i] = INF;

    d[start] = 0;

    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for (int i = 0; i < V; ++i) {
            if (d[i] != INF) continue;
            if (adjacencyMatrix[u][i] == 1)
                d[i] = d[u] + 1;
            Q.push(i);
        }
    }
    return;
}

int main()
{
    cin >> V;

    for (int i = 0; i < V; ++i) {
        int u, k;
        cin >> u >> k;
        assert(u = i + 1);
        for (int j = 0; j < k; ++j) {
            int tmp;
            cin >> tmp;
            adjacencyMatrix[i][tmp - 1] = 1;
        }
    }

    bfs(0);

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

    return 0;
}

C++: //
// Created by 李青坪 on 2018/11/3.
//

#include <cstdio>
#include <algorithm>
#include <queue>

#define MAXN 101
using namespace std;
int n;
int graph[MAXN][MAXN];
int dist[MAXN];
queue<int > q;

void init(){
    fill(dist, dist+MAXN, -1);
    dist[1] = 0;
}

void bfs(int i){
    for (int j = 1; j <= n; ++j) {
        if (graph[i][j] == 1){
            q.push(j);
            dist[j] = dist[j] >= 0 ? min(dist[j], dist[i] + 1): dist[i] + 1;
        }
    }
    while (!q.empty()){
        int next = q.front();
        q.pop();
        bfs(next);
    }
}

int main(){
    scanf("%d", &n);
    int u, k;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        int node;
        for (int j = 0; j < k; ++j) {
            scanf("%d", &node);
            graph[u][node] = 1;
        }
    }
    init();
    bfs(1);
    for (int l = 1; l <= n; ++l) {
        printf("%d %d\n", l, dist[l]);
    }
}
/*
5
1 2 2 4
2 1 4
3 0
4 1 3
5 1 4
 */

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++: 
#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>

auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;

namespace std {
	template<class Ty, size_t Ix>
	struct rank<std::array<Ty,Ix>>
		: integral_constant<size_t, rank<Ty>::value + 1>
	{	// determine number of dimensions of array _Ty
	};
}

template<typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T& arr, const U& v) {
	arr = v;
}
template<typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR& arr, const U& v) {
	for (auto& i : arr) {
		fill_all(i, v);
	}
}

int32_t N;
int32_t INF = 100100;
std::array<std::array<int32_t, 100>,100> dp;
std::array<std::array<int32_t, 100>, 100> dp2;
void WF() {
	for (size_t k = 0; k < N; k++)
		for (size_t i = 0; i < N; i++)
			for (size_t j = 0; j < N; j++)
	{
		dp[i][j] = std::min(dp[i][j], dp[i][k] + dp[k][j]);
	}
}

int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	fill_all(dp, INF);
	in >> N;
	for (int32_t i = 0; i < N; i++)
	{
		int from,m;
		in >> from >> m; --from;
		dp[from][from] = 0;
		for (int32_t j = 0; j < m; j++)
		{
			int to;
			in >> to; --to;
			dp[from][to] = 1;
		}
	}

	WF();
	dp2 = dp;
	WF();
		for (int32_t i = 0; i < N; i++)
		{
			out << i + 1 << ' ' << (dp[0][i]!=INF? dp[0][i]:-1) << endl;
		}

	return 0;
}
#endif

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

using namespace std;
#define MAX 100

const int INF = (1<<21);
int mat[MAX][MAX] = {};
int n, d[MAX] = {};
bool c[MAX];

void bfs(int u){
    queue<int> p; 
    int now = 0;
    d[u] = now++;
    c[u] = true;

    p.push(u);
    while(!p.empty()){
        int x = p.front(); p.pop();
        for(int i = 0; i < n; i++){
            if(mat[x][i] && !c[i]){
                p.push(i);
                c[i] = true;
                d[i] = d[x]+1;
            }
        }
    }
}

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i++){
        int x, k, y;
        cin >> x >> k;
        while(k-- > 0){
            cin >> y;
            mat[x-1][y-1] = 1;
        }
    }

    memset(d, INF, MAX);
    bfs(0);

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