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 <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++: #include<iostream>
#include<queue>
using namespace std;
int N;
int D[101],d[101][101];
void bfs(int src){
    int u[101],k[101],a[101];
    bool visit[101];
    for(int i=1;i<=N;++i){
        cin>>u[i]>>k[i];
        for(int j=0;j<k[i];++j){
            cin>>a[j];
            d[u[i]][a[j]]=1;
        }
        visit[i]=true;
    }
    queue<int>Q;
    Q.push(src);
    D[src]=0;
    visit[src]=false;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=1;i<=N;++i){
            if(d[cur][i]==1&&visit[i]){
                visit[cur]=false;
                visit[i]=false;
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    for(int i=1;i<=N;++i){
        if(i!=1&&D[i]==0){
            D[i]=-1;
        }
        cout<<i<<" "<<D[i]<<endl;
    }
}
int main(){
    cin>>N;
    int start=1;
    bfs(start);
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define max 101
#define INF -1
#define WHITE 0
#define GRAY -1
#define BLACK 1
int n,color[max],d[max];
int varry[max][max];
void bfs(int s){
  queue<int>q;
  int u;
  for(int i=1;i<=n;i++){
    d[i]=INF;
  }
  color[s]=GRAY;
  d[s]=0;
  q.push(s);
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int i=1;i<varry[u][0]+1;i++){
      if(color[varry[u][i]]==WHITE){
	color[varry[u][i]]=GRAY;
	d[varry[u][i]]=d[u]+1;
	q.push(varry[u][i]);
      }
    }
    color[u]=BLACK;
  }

}
int main(){
  int u;
  cin>>n;
  for(int k=0;k<n;k++){
    cin>>u;
    cin>>varry[u][0];
    for(int i=0;i<varry[u][0];i++)
      cin>>varry[u][i+1];
  }
  bfs(1);
  for(int i=1;i<=n;i++)
    cout<<i<<" "<<d[i]<<endl;


  return 0;
}

C++: // ALDS1_11_C
#include <bits/stdc++.h>
#define endl "\n"
#define rep(i,n) for(int i=0;i<int(n);i++)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ipair;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
namespace you {
    std::string to_string(char val) {
        return std::string(1, val);
        // return std::string{val};
        // you::to_string(char)
    }
}

vector<int> d(110,-1);
vector<vector<int>> to(110);
vector<int> setu(110,0);
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);

    int n;
    cin>>n;
    rep(i,n){
        int u,k;
        cin>>u>>k;
        setu[u]=k;
        rep(j,k){
            int q;
            cin>>q;
            to[u].emplace_back(q);
        }
    }
    queue<int> next;
    next.push(1);
    d[1]=0;
    while(!next.empty()){
        int cur=next.front();
        next.pop();
        for(int i=0;i<setu[cur];i++){
            int t=to[cur][i];
            //１に近いところから見ているので、もし距離が更新されているならそれ以上距離が短くなることはない。
            //よって、そのnodeからの探索はしなくていい。a⇔bのときに無限ループしちゃう。
            if(d[t]!=-1) continue; 
            d[t]=d[cur]+1;
            next.push(t);
        }
    }
    for(int i=1;i<=n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
    return 0;
}