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 <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
//#define INF 1e18
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;

vector<int> E[SIZE];
int d[SIZE];

void bfs(int s){
  queue<int> que;
  que.push(s);
  fill(d,d+SIZE,INF);
  d[s] = 0; 
  while(!que.empty()){
    int v = que.front();que.pop();
    for(int to : E[v]) if(d[to] == INF){
      d[to] = d[v]+1;
      que.push(to);
    }
  }
}

int main(){
  cin >> n;
  rep(i,0,n){
    int u,k;
    cin >> u >> k;
    rep(i,0,k){
      int v;
      cin >> v;
      E[u].push_back(v);
    }
  }
  bfs(1);
  rep(i,1,n+1){
    cout << i << " ";
    if(d[i] == INF)cout << -1 << endl;
    else cout << d[i] << endl;
  }

  return 0;
}

C++: // 幅優先探索
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

#include<queue>

using namespace std;

#define MAX_N 100
// 値の定義
const int INV = -1;
const int EXIST = 1;
const int NO_EXIST = 0;

const int YES = 0;
const int NO = -1;

// グラフ情報を保持する配列
int ary_graph[MAX_N + 1][MAX_N + 1];

// 頂点１からの距離を保持する配列
int vertex_distance[MAX_N + 1];
// 頂点１からの訪問状態を保持する配列
int ary_visit[MAX_N + 1];

int n = 0;
int nt[MAX_N + 1];

void widthFirstSearch(int start_vertex);

int main(void)
{
    int u, num, v_num;

    for(int i = 0; i < MAX_N + 1; i++)
    {
    	vertex_distance[i] = 0;
        ary_visit[i] = NO;

        for(int j = 0; j < MAX_N + 1; j++)
        {
            // 配列の初期化
            ary_graph[i][j] = NO_EXIST;
        }
    }

    cin >> n;

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

        // 0オリジンにする
        u--;

        for(int j = 0; j < num; j++)
        {
            // 隣接点を記憶する
            cin >> v_num;

            // 0オリジンにする
            v_num--;
            ary_graph[u][v_num] = EXIST;
        }
    }

    // 頂点１から幅優先探索を実施
    widthFirstSearch(0);

    // 結果の表示
    for(int i = 0; i < n; i++)
    {
        if( i != 0 && vertex_distance[i] == 0 )
        {
            // 開始点から到達不可能な点は, 距離に-1を設定
            // ただし, 開始点自身は除く
            vertex_distance[i] = INV;
        }

        printf("%d %d\n", i + 1, vertex_distance[i]);
    }

    return 0;
}

// u に隣接する v を番号順に取得
int next(int u)
{
    int ret = INV;

    for(int v = nt[u]; v < n; v++)
    {
        nt[u] = v + 1;

        if( ary_graph[u][v] == EXIST )
        {
            // 隣接点が存在する
            ret = v;
            break;
        }
    }

    return ret;
}

// 幅優先探索を実施する関数
// start_vertex : 起点となる頂点のNo.
// vertex_num : グラフの頂点数
void widthFirstSearch(int start_vertex)
{

    queue<int> Q;

    Q.push(start_vertex);
    vertex_distance[start_vertex] = 0;
    ary_visit[start_vertex] = YES;

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

        for(int i = 0; i < n; i++)
        {
            // 隣接点を全て検索する
            int v = next(u);

            if( v == INV )
            {
                // 隣接点が無くなったらキューから削除して, 処理終了
                Q.pop();
                break;
            }

            if( ary_visit[v] == NO
                && ary_graph[u][v] == EXIST )
            {
                // 訪問済み
                ary_visit[v] = YES;

                Q.push(v);
                vertex_distance[v] = vertex_distance[u] + 1;
            }
        }
    }

    return;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;


int main(){
        int n;
        cin >> n;
        vector<vector<int>> Graph(n,vector<int>());
        rep(i,n){
                 int u,k;
                cin >>u >>k;
                --u;
                rep(j,k){
                         int p;
                        cin >> p;
                        --p;
                        Graph.at(u).push_back(p);
                }
        }
        
        vector<int> seen(n,-1);
        queue<int> q;
        q.push(0);
        seen.at(0) = 0;
        while(!q.empty()){
                int now = q.front();
                q.pop();
                for(int p:Graph.at(now)){
                        if(seen.at(p) >= 0) continue;
                        seen.at(p) = seen.at(now) + 1;
                        q.push(p);
                }
        }
        
        rep(i,n){
                 cout << i+1 << " " << seen.at(i) << endl;
        }                

        return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

typedef long long int ll;

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

const ll INF=(ll)1e19;
//const int INF=(1<<30);

int main(){
  int n;
  cin >> n;

  vector<vector<int>> edge(n,vector<int>());
  REP(i,n){
    int node, k;
    cin >> node;
    cin >> k;
    REP(j,k){
      int v;
      cin >> v;
      v--;
      edge[i].emplace_back(v);
    }
  }

  VI dist(n,-1);

  queue<int> q;
  q.push(0);
  dist[0]=0;
  
  while(!q.empty()){
    int v = q.front(); q.pop();

    for(auto &iter: edge[v]){
      if(dist[iter]!=-1)continue;

      dist[iter] = dist[v]+1;
      q.push(iter);
    }
  }

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