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>
#include <iomanip>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
#define vecin(a) rep(i,a.size())cin >> a[i]
#define vecout(a) rep(i,a.size())cout << a[i] << " ";cout << "\n"
#define vecvec(a) vector<vector<(a)>>
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const ld pi = 3.14159265358;
const ll mod = 1000000007;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
template<class T> inline bool change(T a,T b){if(a > b){swap(a,b);return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}
const ll cmax = 1000000;
ll fac[cmax], finv[cmax], inv[cmax];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < cmax; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
ll nCk(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N;
    cin >> N;
    graph G(N);
    ll ten,kazu,to;
    rep(i,N){
        cin >> ten;ten--;
        cin >> kazu;
        rep(j,kazu){
            cin >> to;to--;
            G[ten].push_back(to);
        }
    }
    queue<ll> que;
    vector<ll> dist(N,INF);
    dist[0] = 0;
    que.push(0);
    while(que.size()){
        ll n = que.front();que.pop();
        for(auto x : G[n]){
            if(dist[x] == INF){
                que.push(x);
                dist[x] = dist[n] + 1;
            }
        }
    }
    rep(i,N){
        if(dist[i] == INF){
            cout << i + 1 << " " << -1 << endl;
        }
        else cout << i + 1 << " " << dist[i] << endl;
    }
}

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

using namespace std;

int main()
{
    int n, u, k, v, dis[110];
    bool vis[110];
    memset(dis, -1, sizeof(dis));
    memset(vis, false, sizeof(vis));
    vector<int> adj[110];
    queue<pair<int, int>> que; //point, dis
    cin>>n;
    for (int i=0; i<n; i++)
    {
        cin>>u>>k;
        for (int j=0; j<k; j++)
        {
            cin>>v;
            adj[u].push_back(v);
        }
    }
    que.push({1, 0});
    vis[1]=true;
    dis[1]=0;
    while (!que.empty())
    {
        int a=que.front().first, b=que.front().second;
        que.pop();
        for (auto u : adj[a])
        {
            if (!vis[u])
            {
                vis[u]=true;
                dis[u]=b+1;
                que.push({u, b+1});
            }
        }
    }
    for (int i=1; i<=n; i++) cout<<i<<' '<<dis[i]<<'\n';
    return 0;
}

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

using namespace std;

int main(){
  int n;
  cin >> n;
  vector<vector<bool> > a(n+1, vector<bool>(n+1, false));
  for(int i = 1; i <= n; i++){
    int u;
    cin >> u;
    int k;
    cin >> k;
    for(int j = 0; j < k; j++){
      int v;
      cin >> v;
      a[i][v] = true;
    }
  }
  // ??????1??????????????¢???????????????
  // ?????????????????¨??????0??\???, ???????????????????????°-1
  vector<int> d(n+1, -1);
  d[1] = 0;

  queue<int> q;
  q.push(1);

  while(!q.empty()){
    int current = q.front();
    q.pop();
    for(int dst = 1; dst <= n; dst++){
      if(d[dst] < 0 && a[current][dst]){
        d[dst] = d[current] + 1;
        q.push(dst);
      }
    }
  }

  for(int i = 1; i <= n; i++){
    cout << i << " " << 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;

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

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

int n = 0;

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] = INV;

        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++)
    {
        printf("%d %d\n", i + 1, vertex_distance[i]);
    }

    return 0;
}

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

    queue<int> Q;

    // 開始点の情報の設定
    Q.push(start_vertex);
    vertex_distance[start_vertex] = 0;

    while( !Q.empty() )
    {
        // キューの先頭要素を取得
        int u = Q.front();
        Q.pop();

        for(int v = 0; v < n; v++)
        {

            if( ary_graph[u][v] == EXIST && vertex_distance[v] == INV )
            {

                // ----- 未訪問の頂点が見つかった -----

                // 距離情報の更新
                vertex_distance[v] = vertex_distance[u] + 1;

                // キューにプッシュする
                Q.push(v);
            }
        }
    }

    return;
}