Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

C++: /*
    Problem 28
    https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 105;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
vector<vector<int>> G(MAX, vector<int>(MAX));
vector<int> dist(MAX, -1);
/* function */
void bfs() {
    queue<int> q;
    q.push(0);
    dist[0] = 0;  // distで訪れたかどうかcheck
    while (!q.empty()) {
        int from = q.front();
        q.pop();
        for (int to : G[from]) {
            if (dist[to] != -1) continue;
            dist[to] = dist[from] + 1;
            q.push(to);
        }
    }
}
/* main */
int main(){
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int u, k;
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            v--;
            // 有効グラフ
            G[u].push_back(v);
        }
    }

    bfs();

    for (int i = 0; i < N; i++) cout << i+1 << ' ' << dist[i] << '\n';
}