Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAX=200;
vector<int> T[MAX],state(MAX),d(MAX,-1);
int N;
queue<int> Q;

void BFS(){
    Q.push(0);
    d[0]=0;
    state[0]=1;
    while(Q.size()){
        int a=Q.front();
        for(int i=0;i<T[a].size();i++){
            if(state[T[a][i]]==0){
            Q.push(T[a][i]);
            state[T[a][i]]=1;
            d[T[a][i]]=d[a]+1;
        }
        }
        Q.pop();
    }
    for(int i=0;i<N;i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
}

int main(){

    cin>>N;
    
    for(int i=0;i<N;i++){
        state[i]=0;
    }
    for(int i=0;i<N;i++){
        int x,y;cin>>x>>y;
        x--;
    for(int j=0;j<y;j++){
        int a;cin>>a;
        a--;;
        T[x].push_back(a);
    }
    }
    BFS();
}

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 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 P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
    ll N;
    cin >> N;
    Graph graph(N);
    rep(i, N) {
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            ll v;
            cin >> v;
            v--;
            graph[u].push_back(v);
        }
    }
    vector<ll> dist(N, -1);
    dist[0] = 0;
    queue<P> que;
    que.push({0, 0});
    while (!que.empty()) {
        P p = que.front();
        que.pop();
        for (ll v: graph[p.first]) {
            if (dist[v] == -1) {
                dist[v] = p.second + 1;
                que.push({v, dist[v]});
            }
        }
    }
    rep(i, N) {
        cout << i + 1 << ' ' << dist[i] << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}

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

int n;
vector<vector<int>> M(100, vector<int>(100));
vector<int> C(100);
vector<int> D(100);


void bfs(int v) {
  C.at(v) = GRAY;
  D.at(v) = 0;
  queue<int> Q;
  Q.push(v);

  while (Q.size() > 0) {
    int u = Q.front(); Q.pop();
    C.at(u) = BLACK;
    rep(i, n) {
      if (M.at(u).at(i) == 1 && C.at(i) == WHITE){
        Q.push(i);
        C.at(i) = GRAY;
        D.at(i) = D.at(u)+1;
      } 
    }
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    int id, k; cin >> id >> k;
    rep(j, k) {
      int v; cin >> v;
      M.at(i).at(v-1) = 1;
    }
  }
  rep(i, n)D.at(i) = -1;
  bfs(0);
  rep(i,n) cout << i+1 << " " << D.at(i) << endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif


int main(int argc, char **argv){
	int n, u, k, v;
	std::vector<std::vector<int> > G;
	std::vector<int> min_cost;
	std::vector<bool> is_searched;

#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
	//FILE *fp = fopen("test.txt", "r");
#endif

#if INPUT_FROM_FILE
	ifs >> n;
#else
	std::cin >> n;
#endif

	G.resize(n);
	is_searched.resize(n, false);
	min_cost.resize(n, -1);

	for(int i = 0; i < n; i++){
#if INPUT_FROM_FILE
		ifs >> u >> k;
#else
		std::cin >> u >> k;
#endif
		G[u - 1].resize(k);
		for(int j = 0; j < k; j++){
#if INPUT_FROM_FILE
			ifs >> v;
#else
			std::cin >> v;
#endif
			G[u - 1][j] = v - 1;
		}
	}

	min_cost[0] = 0;

	std::queue<int> que;
	
	que.push(0);
	while(!que.empty()){
		int n = que.front(); que.pop();

		for(int i = 0; i < G[n].size(); i++){
			int nn = G[n][i];
			
			if(min_cost[nn] < 0 || min_cost[nn] > min_cost[n] + 1){
				min_cost[nn] = min_cost[n] + 1;
				que.push(nn);
			}
		}
	}

	for(int i = 0; i < n; i++){
		std::cout << i + 1 << " " << min_cost[i] << std::endl;
	}


	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------

int n;
bool g[110][110];
int d[110];

void bfs(int s)
{
	d[s] = 0;
	queue<int> q;
	q.push(s);

	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = 1; i <= n; i++) {
			if (g[u][i] && d[i] == INF) {
				q.push(i);
				d[i] = d[u] + 1;
			}
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			g[u][v] = true;
		}
	}

	for (int i = 1; i <= n; i++) d[i] = INF;

	bfs(1);

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

	return 0;
}