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<k;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 <cstdio>
#include <queue>
using namespace std;
#define INF (1<<21)

int n, G[100][100], dist[100], visited[100];


int main(void) {
    int u, k, v, i, j;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++) G[i][j] = 0;

    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        u--;
        for (j = 0; j < k; j++) {
            scanf("%d", &v);
            v--;
            G[u][v] = 1;
        }
    }
    
    for (i = 1; i < n; i++) {
        dist[i] = INF;
        visited[i] = 0;
    }
    dist[0] = 0;
    visited[0] = 1;

    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        u = q.front();
        q.pop();
        for (i = 0; i < n; i++) {
            if (G[u][i] == 0 || visited[i] == 1) continue;
            q.push(i);
            visited[i] = 1;
            dist[i] = dist[u] + 1;
        }
    }

    for (i = 0; i < n; i++) {
        if (dist[i] == INF) dist[i] = -1;
        printf("%d %d\n", i + 1, dist[i]);
    }

    return 0;
}

C++: //Breadth First Search
#include <iostream>
#include <queue>

using namespace std;

static const int N = 100;

int d[N];

void bfs(vector<vector<int>> &graph, int u) {
    queue<int> q;
    q.push(u);
    d[u] = 0;

    while(!q.empty()) {
        int v = q.front();
        q.pop();
        for (auto w: graph[v]) {
            if(d[w] != -1) continue;
            q.push(w);
            d[w] = d[v] + 1;
        }
    }
}

int main() {
    int n;
    cin >> n;
    int u, k, v;
    vector<vector<int>> graph(n);
    for(int i=0; i<n; i++) {
        cin >> u >> k;
        for(int j=0; j<k; j++) {
            cin >> v;
            graph[u-1].push_back(v-1);
        }
    }
    for(int i=0; i<n; i++) d[i] = -1;

    bfs(graph, 0);

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

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

#define N 100

int n, M[N][N], d[N];

queue<int> Q;

void bfs(int x){
    //初期化
    rep(i,n){
        d[i] = -1;
    }

    d[x] = 0;
    Q.push(x);

    while(!Q.empty()){
        int v = Q.front(); Q.pop();
        for(int i = 0; i < n; i++){
            if(M[v][i] == 1){
                if(d[i] == -1){
                    Q.push(i);
                    d[i] = d[v] + 1;
                }else{
                    d[i] = min(d[i], d[v] + 1);
                }
                
            }
        }
    }
}


int main(){
    cin >> n;

    rep(i,N)rep(j,N) M[i][j] = 0;

    int u, k, v;
    rep(i,n){
        cin >> u >> k;
        u--; //0オリジンに変換
        rep(j,k){
            cin >> v;
            v--; //0オリジンに変換
            M[u][v] = 1;
        }
    }

    bfs(0);

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

    return 0;
    
}

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

using namespace std;
int main(){
  int u, i, id, k, j;
  cin >> u;
  unordered_map<int, vector<int> > mymap;
  queue<vector<int> > myqueue;
  for (i = 0; i < u; ++i){
    cin >> id >> k;
    vector<int> points;
    for (j = 0; j < k; ++j){
      int a;
      cin >> a;
      points.push_back(a);
    }
    mymap[id] = points;
    if(id == 1){
      myqueue.push(points);
    }
  }
  
  bool visited[u+1];
  for (j = 1; j <= u; ++j){
    visited[j] = false;
  }
  visited[1] = true;

  int dist[u+1];
  for (j = 1; j <= u; ++j){
    dist[u] = -1;
  }
  dist[1] = 0;
  int d = 1;

  while(!myqueue.empty()){
    vector<int> combined;
    for (i = 0; i < myqueue.front().size(); ++i){
      if(!visited[myqueue.front()[i]]){
        dist[myqueue.front()[i]] = d;
        visited[myqueue.front()[i]] = true;
        if(combined.empty()){
          combined = mymap[myqueue.front()[i]];
        }else{
          combined.insert(combined.end(), mymap[myqueue.front()[i]].begin(), mymap[myqueue.front()[i]].end());
        }
      }
    }
    if(!combined.empty()){
      myqueue.push(combined);
    }
    myqueue.pop();
    d++;
  }

  for (j = 1; j <= u; ++j){
    cout << j << " " << dist[j] << endl;
  }

}

C++: #include<iostream>
#include<queue>
using namespace std;

int main()
{
    queue<int> Q;
    int n;
    int a, b, c;
    cin >> n;
    int D[100], in[100][100];
    for (int i = 0; i < n; i++)
    {
        D[i] = -1;
        for (int j = 0; j < n; j++)
            in[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        for (int j = 0; j < b; j++)
        {
            cin >> c;
            in[c - 1][a - 1] = 1;
        }
    }
    Q.push(0);
    D[0] = 0;
    while (!Q.empty())
    {
        int cur = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++)
        {
            if (in[i][cur] == 1 && D[i] == -1) {
                D[i] = D[cur] + 1;
                Q.push(i);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << D[i] << endl;
    }
    return 0;
}