Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

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

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

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
		}
	}
}

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

C++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
#define all(x) (x).begin(),(x).end()


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, e;
    cin >> n;
    vector<vector<int>> vec(n+1, vector<int>());
    for(int i = 0; i < n; i++){
      int u, k;
      cin >> u >> k;
      for(int j = 0; j < k; j++){
          cin >> e;
          vec[u].push_back(e);
      }
    }

    vector<int> cost(n+1, -1);
    queue<int> que;
    que.push(1);
    cost[1] = 0;
    int num;
    while(!que.empty()){
      num = que.front(); que.pop();
      for(int i=0; i<vec[num].size(); i++){
          int next = vec[num][i];
        if(cost[next] == -1){
          cost[next] = cost[num] + 1;
          que.push(next);
        }
      }
    }
    for(int i=1; i<=n; i++){
      cout << i << " " <<  cost[i] << ln;
    }
}

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[u] != 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 <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

const ll mod = 1e9 + 7;

// Union-Find木
// 初期化 UF(N); Nは要素数
// 結合 unite(x,y); 同じ集合か判定 same(x,y);
// 集合の数 count
struct UnionFind {
  vector<ll> par, sizes;
  ll cnt;
  UnionFind(ll N) : par(N), sizes(N, 1), cnt(N) {
    for (ll i = 0; i < N; i++) par[i] = i;
  }

  ll find(ll x) { return par[x] == x ? x : par[x] = find(par[x]); }

  bool same(ll x, ll y) { return find(x) == find(y); }

  ll size(ll x) { return sizes[find(x)]; }

  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (sizes[x] < sizes[y]) swap(x, y);
    par[y] = x;
    sizes[x] += sizes[y];
    cnt--;
  }

  ll count() const { return cnt; }
};

int main() {
  ll n;
  cin >> n;

  vvll eds(n);

  rep(i, n) {
    ll u, k;
    cin >> u >> k;
    u--;
    rep(j, k) {
      ll v;
      cin >> v;
      v--;
      eds[u].push_back(v);
    }
  }

  vll dist(n, -1);
  queue<ll> q;
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    ll u = q.front();
    q.pop();
    for (auto& v : eds[u]) {
      if (dist[v] == -1) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }  // v
  }

  rep(i, n) printf("%lld %lld\n", i + 1, dist[i]);

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

using Lint=long long;
using Graph=vector<vector<int>>;

const int INF=1e9;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin>>N;
    Graph G(N);
    for(int i=0;i<N;i++){
        int u,k; cin>>u>>k;
        u--;
        while(k--){
            int v; cin>>v;
            v--;
            G[u].push_back(v);
        }
    }

    vector<int> dist(N,INF);
    dist[0]=0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int v=Q.front(); Q.pop();
        for(int u:G[v]){
            if(chmin(dist[u],dist[v]+1)) Q.push(u);
        }
    }
    for(int i=0;i<N;i++){
        cout<<i+1<<' '<<(dist[i]==INF? -1:dist[i])<<endl;
    }
    return 0;
}