Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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]);
}