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 <vector>
#include <queue>
using namespace std;
#define MAX_V 110

int main() {
    
    vector<vector<int>> G(MAX_V);
    int n; cin >> n;
    for(int i=0; i<n; i++){
        int v, k; cin >> v >> k;

        for(int j=0; j<k; j++){
            int u; cin >> u;
            G[v].push_back(u);
        }
    }

    vector<int> dist(MAX_V, -1);
    dist[1] = 0;

    queue<int> q;
    q.push(1);
    while(!q.empty()){

        int v = q.front();
        q.pop();

        for(int i=0; i<G[v].size(); i++){
            int u = G[v][i];
            if(dist[u] == -1){
                dist[u] = dist[v] + 1;
                q.push(u);
            }
        }
    }

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

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

template<class T>
struct Edge{
	int from,to;
	T cost;
	Edge(int to,T cost) : to(to),cost(cost){}
	Edge(int from,int to,T cost) : from(from),to(to),cost(cost){}
};

template<class T>
using WeightedGraph = vector<vector<Edge<T>>>;
using Graph = vector<vector<int>>;
template<class T>
using Matrix = vector<vector<T>>;

template<class T = int>
vector<T> BFS(const Graph &G,int source = 0){
	vector<T> dist(G.size(),-1);
	queue<int> que;
	dist[source] = 0;
	que.push(source);
	for(T d = 0;;d++){
		int siz = que.size();
		if(siz == 0) break;
		for(int i = 0;i < siz;i++){
			int v = que.front();que.pop();
			for(int to : G[v]){
				if(dist[to] == -1){
					dist[to] = d + 1;
					que.push(to);
				}
			}
		}
	}
	return dist;
}

signed main(){
	int n;
	cin >> n;
	Graph G(n);
	for(int i = 0;i < n;i++){
		int u,k;
		cin >> u >> k;
		for(int j = 0;j < k;j++){
			int v;
			cin >> v;
			G[u - 1].push_back(v - 1);
		}
	}
	auto dist = BFS(G,0);
	for(int i = 0;i < n;i++) cout << i + 1 << " " << dist[i] << endl;
}

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

const int INF = 1001001001;
int n;
int v[100][100];
int d[100]; //?§??????????????????¢

void bfs(int s) //s????§????
{
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i++) d[i] = INF;

  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();

    for (int i = 0; i < n; i++) {
      if (v[u][i] == 0) continue;
      if (d[i] != INF) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }

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

signed main()
{
  int u, k;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      int tmp;
      cin >> tmp;
      tmp--;
      v[i][tmp] = 1;
    }
  }

  bfs(0);

  return (0);
}

C++: #include <bits/stdc++.h>
using namespace std;
void main_sub(void);
int main(int argc, char* argv[]) {
    main_sub();
    return 0;
}
// int を int64_t にする
// g++ に -DUSE_STRICT_INT をつけるか、
// 下のコメントアウトを外すと無効化
// #define USE_STRICT_INT
using i64 = int64_t;
#ifndef USE_STRICT_INT
#define int i64
#endif
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = (1 << 30);
constexpr i64 INFL = (1LL << 62);
constexpr i64 MOD = 1000000007;

void main_sub(void) {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    rep(i, n) {
        int u, k;
        cin >> u;
        u--;
        cin >> k;
        rep(j, k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    queue<pair<int, int>> q;
    vector<int> dist(n, INF);
    q.push({0, 0});
    while (!q.empty()) {
        int from, d;
        tie(from, d) = q.front();
        q.pop();
        if (dist[from] > d) {
            dist[from] = d;
        }
        for (auto& to : g[from]) {
            if (dist[to] > d + 1) {
                dist[to] = d + 1;
                q.push({to, d + 1});
            }
        }
    }
    rep(i, n) {
        cout << i + 1 << " " << (dist[i] == INF ? -1 : dist[i]) << endl;
    }

    return;
}