Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define prime 1000000007
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
signed main(void) {
    int n;
    cin >> n;
    vector<int> Graph[n];
    rep(i, n) {
        int k;
        cin >> k >> k;
        rep(j, k) {
            int a;
            cin >> a;
            Graph[i].push_back(a - 1);
        }
    }

    vector<int> dist(n, -1);
    dist[0] = 0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()) {
        int now = Q.front();
        Q.pop();
        for(auto x : Graph[now]) {
            if(dist[x] != -1) continue;
            dist[x] = dist[now] + 1;
            Q.push(x);
        }
    }
    rep(i, n) cout << i + 1 << ' ' << dist[i] << endl;
}

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

#define MAX 200
int n;
vector<vector<int>> adj_mat(MAX, vector<int>(MAX, 0));

queue<int> q;
vector<int> visited(MAX, -1);
vector<int> d(MAX, -1);

void add_queue(int node){
    // cout << "node " << node << " unvisited & adj";
    rep(i, n){
        if(adj_mat.at(node).at(i) == 1 && visited.at(i) == -1){
            // nodeに隣接し、まだ訪れていないノード
            q.push(i);
            d.at(i) = d.at(node) + 1;
            visited.at(i) = 1;
            // cout << " " << i;

        }
    }
    // cout << endl;
}

void bfs(int node){
    q.push(node);
    d.at(node) = 0;
    visited.at(node) = 1;
    while(q.size() != 0){
        int visited_node = q.front();
        q.pop();
        // visited.at(visited_node) = 1;
        add_queue(visited_node);

    }
}

int main(){
    cin >> n;

    // 隣接行列の作成
    rep(i, n){
        int u, k; cin >> u >> k;
        rep(j, k){
            int v; cin >> v;
            adj_mat.at(u-1).at(v-1) = 1;
        }
    }

    bfs(0);
    rep(i, n){
        cout << i+1 << " " << d.at(i) << endl;
    }

}

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 argc, char const *argv[])
{
    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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vsi = vector<queue<int>>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void bfs(vsi &g, vi &d, queue<int> &q, int v, int depth) {
  queue<int> nq;
  while (!q.empty()) {
    int f = q.front(); q.pop();

    while (!g[f].empty()) {
      nq.push(g[f].front()); g[f].pop();
    }

    if (d[f] == -1) d[f] = depth;
  }
  
  q = nq;
  if (q.empty()) return;
  bfs(g, d, q, q.front(), depth + 1);
}

int main() {
  int n;
  cin >> n;
  
  vsi g(n);
  rep(i, n) {
    int k, u;
    cin >> k >> u;

    rep(j, u) {
      int v;
      cin >> v;

      g[i].push(v - 1);
    }
  }

  vi d(n, -1);
  queue<int> q;
  q.push(0);
  bfs(g, d, q, 0, 0);

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