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) {
			if !dl.isVisited(v) {
				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++: #include <iostream> 
#include <queue>
#define NODE_NUM 100
using namespace std;

int v_num, v_degree, v, v_id;
int adj_M[NODE_NUM][NODE_NUM], Distance[NODE_NUM], visit_state[NODE_NUM];
static const int WHITE = 0;
static const int GRAY  = 1;
static const int BLACK = 2;

void BreadthFirstSearch(int node) 
{
    queue<int> Q;
    Q.push(node);
    Distance[node] = 0;
    visit_state[node] = GRAY;

    while(! Q.empty())
    {
        int cur_node = Q.front();
        for (int i = 0; i < v_num; ++i) 
        {
            if (adj_M[cur_node][i] == 1 && visit_state[i] == WHITE) 
            {
                Q.push(i);
                visit_state[i] = GRAY;
                Distance[i] = Distance[cur_node] + 1;
            } 
        } 
        Q.pop();
        visit_state[cur_node] = BLACK;
    }
}

int main() 
{
    cin >> v_num;

    // init
    for (int i = 0; i < v_num; ++i) 
    {
        for (int j = 0; j < v_num; ++j) 
        {
            adj_M[i][j] = 0;
        }
        Distance[i] = -1;
        visit_state[i] = WHITE;
    }

    for (int i = 0; i < v_num; ++i) 
    {
        cin >> v_id >> v_degree;
        v_id--;
        for (int i = 0; i < v_degree; ++i) 
        {
            cin >> v;
            adj_M[v_id][v-1] = 1;
        }
    }

    BreadthFirstSearch(0);

    for (int i = 0; i < v_num; ++i) cout << i+1 << " " << Distance[i] << endl;
}

C++: #include<iostream>

#include<algorithm>

#include<vector>

#include<cassert>

#include<cstdio>

#include<queue>



using namespace std;



int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離



void bfs(int src){

	queue<int> Q;

	Q.push(src);

	fill(D, D + N, -1);

	D[src] = 0;

	while (!Q.empty()){

		int cur = Q.front();

		Q.pop();

		for (int k = 0; k < N; k++){

			if (G[cur][k] == 1 && D[k] == -1){

				D[k] = D[cur] + 1;

				Q.push(k);

			}

		}

	}

	for (int l = 0; l < N; l++){

		cerr << l + 1 << " " << D[l] << endl;

	}

}



int main(){

	scanf_s("%d", &N);

	int id, c;

	for (int i = 0; i < N; i++){

		scanf_s("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。

		scanf_s("%d", V + id);

		for (int j = 0; j < V[id]; j++){

			scanf_s("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。

			G[id][c] = 1;

		}

	}//データの読み込み完了。

	bfs(0);

}

C++: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9 + 7;
const ll LINF = 1LL << 60;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}

int n;
vector<int> g[100];

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  cin >> n;
  rep(i,n) {
    int u, k;
    cin >> u >> k;
    --u;
    rep(j,k) {
      int v;
      cin >> v;
      --v;
      g[u].push_back(v);
    }
  }
  queue<int> q;
  q.push(0);
  vector<int> dst(n, -1);
  dst[0] = 0;
  while(!q.empty()) {
    int v = q.front();
    q.pop();
    for(int u : g[v]) {
      if(dst[u] != -1) continue;
      dst[u] = dst[v]+1;
      q.push(u);
    }
  }
  rep(i,n) cout << i+1 << " " << dst[i] << '\n';
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T,vector<T>,greater<T>>
#define Pqza(T) priority_queue<T>
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;

//vector の中身を出力
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

const int MAX = 128;

vector <int> adj[MAX];
int d[MAX];

void solve(void){
	ENJYU;
	int N;
	cin >> N;
	rep(N, i) {
		int u, k;
		cin >> u >> k;
		u--;
		rep(k, i) {
			int v;
			cin >> v;
			v--;
			adj[u].pb(v);
		}
	}
	memset(d, -1, sizeof(d));
	queue <int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int n = q.front();
		q.pop();
		for (int nv : adj[n]) {
			if (d[nv] > 0) continue;
			d[nv] = d[n] + 1;
			q.push(nv);
		}
	}

	rep(N, i) cout << i+1 << ' ' << d[i] << endl;

}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}