Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(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';
}

C++: #include <iostream>
#include <climits>
#include <vector>
#include <queue>
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF INT_MAX

using namespace std;

void bfs(int s, int n, int *color, int *d, vector<vector<int>> Adj){
    queue<int> Q;
    for (int u = s+1; u <= n; u++){
        color[u] = WHITE;
        d[u] = INF;
    }
    color[s] = GRAY;
    d[s] = 0;
    Q.push(s);
    while (Q.size() != 0){
        int zero = 0;
        int u = Q.front();
        Q.pop();
        for (int w = 1; w < Adj[u].size(); w++){
            int v = Adj[u][w];
            if (v == 0){
                break;
            }else if (color[v] == WHITE){
                color[v] = GRAY;
                d[v] = d[u] + 1;
                Q.push(v);
                zero++;
            }
        }
        color[u] = BLACK;
    }
}

int main(int argc, char* argv[]) {
    int n, u, k, v;
    cin >> n;
    n++;
    int color[n], d[n], f[n];
    vector<vector<int>> Adj(n, vector<int>(n, 0));

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

    bfs(1, n, color, d, Adj);

    for (int i = 1; i < n; i++){
        if (d[i] == INF)
            printf("%d -1\n", i);
        else
            printf("%d %d\n", i, d[i]);
    }
    return 0;
}

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

map<int, vector<int>> A;
int color[101];
int D[101];
int d = 0;

void dfs(int u) {
    queue<int> Q;
    Q.push(u);
    color[u] = 1;
    int current;
    D[u] = d;
    while(!Q.empty()) {
        current = Q.front();
        Q.pop();
        vector<int>::iterator it = A[current].begin();
        while(it != A[current].end()) {
            if (color[*it] == 0) {
                Q.push(*it);
                color[*it] = 1;
                D[*it] = D[current] + 1;
            }
            it++;
        }
    }
}

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

    int u, k;
    for (int i = 1; i <= n; i++) {
        color[i] = 0;
        D[i] = -1;
        cin >> u >> k;
        int ad;
        for (int j = 0; j < k; j++) {
            cin >> ad;
            A[u].push_back(ad);
        }
    }

    dfs(1);

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