Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

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


#define ALREADY 1
#define YET 0

int *passed;
int *distance_ps;

using namespace std;


void bfs(vector< vector<int> > a, int i, int dis){
  // i?????????vector<int>??????????????¢?????????
  dis++;
  vector<int> ai = a[i];
  int n = ai.size();
  // cout << "i: " << i << ",n: " << n << endl;
  for(int j=0;j<n;j++){
    int nexti=ai[j]-1;
    
    if(passed[nexti]==YET || distance_ps[nexti]>dis){
      passed[nexti]=ALREADY;
      distance_ps[nexti]=dis;
      bfs(a, nexti, dis);
    }
  }
}

int main(){
  int n;
  cin >> n;
  vector< vector<int> > a;
  for(int i=0;i<n;i++){
    int u;
    int k;
    cin >> u >> k;
    vector<int> tmpa;
    for(int j=0;j<k;j++){
      int tmp;
      cin >> tmp;
      tmpa.push_back(tmp);
    }
    a.push_back(tmpa);
  }
  
  int a2[n][n];
  passed = new int[n];
  distance_ps = new int[n];
  
  for(int i=0;i<n;i++){
    passed[i] = 0;
    distance_ps[i] = 1000;
  }

  distance_ps[0]=0;
  bfs(a, 0, 0);

  for(int i=0;i<n;i++){
    if(passed[i]==YET){
      distance_ps[i] = -1;
    }
  }
  distance_ps[0]=0;
  
  for(int i=0;i<n;i++){
    cout << i << " " << distance_ps[i] << endl;
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      a2[i][j]=0;
    }
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<(int)a[i].size();j++){
      a2[i][a[i][j]-1] = 1;
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      // if(j!=0)cout << " ";
      // cout << a2[i][j];
    }
    // cout << endl;
  }
      
  
}

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 <bits/stdc++.h>
using namespace std;

#define REP(i, start, count) for(int i=(start); i<(int)(count); ++i)
#define rep(i, count) REP(i, 0, count)
#define ALLOF(c) (c).begin(), (c).end()

typedef long long ll;
typedef unsigned long long ull;

int n;
vector<vector<int>> G;

int main(void) {

    cin >> n;
    G.push_back(vector<int>(0));

    for (int i = 1; i <= n; ++i) {
        int u, k;
        cin >> u >> k;
        G.push_back(vector<int>());
        for (int m = 0; m < k; ++m) {
            int v;
            cin >> v;
            G[u].push_back(v);
        }
    }

    queue<int> Q;
    vector<int> color(n + 1, 0);
    vector<int> d(n + 1, -1);
    Q.push(1);
    color[1] = 1;
    d[1] = 0;

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

        for (int v : G[idx]) {
            // 未訪問
            if (color[v] == 0) {
                d[v] = d[idx] + 1;
                Q.push(v);
                color[v] = 1;
            }
        }

        color[idx] = 2;
        Q.pop();
    }

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

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

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)
using namespace std;

int main(){
    int n,u,k,v,src=0;
    cin >> n;
    int D[n],g[n][n];
    REP(i,n){
        D[i] = 0;
        REP(j,n){
            g[i][j] = 0;
        }    
    }
    REP(i,n){ 
        cin >> u >> k;
            REP(j,k){
                cin >> v;
                g[v-1][u-1] = 1;
            }
    }

    queue<int> Q;
    Q.push(src);
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
            REP(i,n){
                if(g[i][cur] == 1 && D[i] == 0){
                    D[i] = D[cur] + 1;
                    Q.push(i);
                }
            }
    }

    REP(i,n){
        cout << i+1 << " " << D[i] << endl;    
    }
}

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
 */