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++: #pragma GCC diagnostic ignored "-Wunused-variable" 
#include <bits/stdc++.h>
using namespace std;

//#define BOOST
#ifdef BOOST
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/range/adaptor/reversed.hpp>
using namespace boost;
using ml = boost::multiprecision::cpp_int;
using md = boost::multiprecision::cpp_dec_float_100;
#define forir(i, ...) if(ll i=(ll)v.size())for(__VA_ARGS__)if(i--,1)
#define eachr(i, e, c) forir(i, auto &&e: adaptors::reverse(c))
#endif

/***** type *****/
using ll = long long;
using ld = long double;
using pll = pair<long long, long long>;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;
template <class T> using vvvt = vector<vector<vector<T>>>;
/***** define *****/
#define all(c) (c).begin(), (c).end()            // begin to end
#define coutd cout << fixed << setprecision(10)  // cout double
#define output(x) do{cout<<x;return 0;}while(0)  // cout return
#define rep(i, b, e) for (ll i = b; i < e; i++)  // repeat
#define repr(i, b, e) for (ll i = b; e < i; i--) // repeat reverse
#define fori(i, ...) if (ll i = -1) for(__VA_ARGS__) if (i++, 1)
#define each(i, e, c) fori (i, auto&& e: c)      // for each
/***** const value *****/
#define llong_max 9223372036854775807            // 9 * 10^18
#define ldbl_max 1.79769e+308                    // 1.7 * 10^308
#define pi 3.1415926535897932                    // 3.14 ...
/***** lambda *****/
auto Ceil = [] // if (a % b != 0) return a / b + 1;
(auto x) { return (ll)ceil(x); };
auto Count = [] // long long count value
(auto b, auto e, auto x) { return (ll)count(b, e, x); };
auto CtoL = [] // char to number
(auto c) { return (ll)c - (ll)'0'; };
auto LtoC = [] // number to char
(auto n) { return (char)('0' + n); };
auto Pow = [] // long long pow
(auto a, auto b) { return (ll)pow(a, b); };
auto Pow2 = [] // long long pow2
(auto n) { return (1LL << n); };
auto Pow10 = [] // long long pow10
(auto n) { return (ll)pow(10, n); };
auto Size = []  // long long collection size
(auto& c) { return (ll)(c).size(); };
auto Sum = [] // long long accumulate
(auto b, auto e) { return accumulate(b, e, 0LL); };

/***** template *****/
template <class T> void MakeVVT
(ll ys, ll xs, vvt<T>& v, T fill = T())
{	// vector<vector<T>> resize + fill
	v.resize(ys); rep(y, 0, ys) v[y].resize(xs, fill);
}
template <class T> void MakeVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fill = T())
{	// vector<vector<vector<T>>> resize + fill
	v.resize(zs); rep(z, 0, zs) MakeVVT(ys, xs, v[z], fill);
}
template <class T> void InputVVT
(ll ys, ll xs, vvt<T>& v, T fix = T())
{	// input vector<vector<T>> (T != struct) + fix
	MakeVVT(ys, xs, v, fix);
	rep(y, 0, ys) rep(x, 0, xs) { cin >> v[y][x]; v[y][x] += fix; }
}
template <class T> void InputVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fix = T())
{	// input vector<vector<vector<T>>> (T != struct) + fix
	v.resize(zs); rep(z, 0, zs) InputVVT(ys, xs, v[z], fix);
}

/**************************************/
/********** BEGIN OF NYA LIB **********/
/**************************************/

namespace NyaGadget {}

namespace NyaGadget
{
	/***** BFSライブラリ *****/

	struct GT_NyaaBFS
	{
		/**
		@brief BFS実行関数
		@param start 探索開始頂点
		@param g グラフ
		@param res 結果を格納する構造体
		@note
		 探索開始頂点startからBFSで処理する。
		**/
		void Run(long long start, const std::vector<std::vector<long long>>& g, std::vector<long long>& res)
		{	// 初期化
			std::queue<long long> q;
			q.push(start);
			res.resize(g.size(), -1);
			res[start] = 0;

			while (!q.empty())
			{
				long long from = q.front(); q.pop();
				for (auto&& to : g[from])
				{	// 頂点fromから頂点toへの処理
					if (res[to] != -1) continue;
					res[to] = res[from] + 1;
					q.push(to);
				}
			}
		}

		/**
		@brief BFS実行関数
		@param start 探索開始頂点の集合
		@param g グラフ
		@param res 結果を格納する構造体
		@note
		 探索開始頂点が複数あるとき最初に全てキューに入れる必要がある。
		 したがって、開始頂点はvectorに格納して渡す。
		**/
		void Run(std::vector<long long> start, const std::vector<std::vector<long long>>& g, std::vector<long long>& res)
		{	// 初期化
			std::queue<long long> q;
			for (auto& e : start) q.push(e);
			res.resize(g.size());

			while (!q.empty())
			{
				long long from = q.front(); q.pop();
				for (auto&& to : g[from])
				{	// 頂点fromから頂点toへの処理
					q.push(to);
				}
			}
		}
	};
}

/**************************************/
/*********** END OF NYA LIB ***********/
/**************************************/

using namespace NyaGadget;
//using mll = NT_ModLL< 1000000007 >;
//using mll = NT_ModLL< 998244353 >;

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

	std::vector<std::vector<long long>> g(n);
	rep(i, 0, n)
	{
		ll u, k; cin >> u >> k; u--;
		rep(j, 0, k)
		{
			ll v; cin >> v; v--;
			g[u].push_back(v);
		}
	}
	GT_NyaaBFS bfs;
	std::vector<long long> res;
	bfs.Run(0, g, res);
	each(i, e, res) cout << i+1 << " " << e << endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
#define N 100
#define NIL -1

int M[N][N];
int d[N];
int n;

// 初期化処理後、sを開始地点として幅優先探索を行い、探索結果を出力する
// d[N]にはsからの距離を記録し(sは0)、sからたどり着けない頂点はNIL(空)を記録する
void bfs(int s)
{
	for (int i = 0; i < n; i++)d[i] = NIL;

	std::queue<int> Q;
	Q.push(s);
	d[s] = 0;

	while (!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		for (int v = 0; v < n; v++)
		{
			if (M[u][v] == 0)continue;
			if (d[v] != NIL) continue;
			Q.push(v);
			d[v] = d[u] + 1;
		}
	}

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

int main()
{
	std::cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			M[i][j] = 0;
		}
	}

	int u, v, k;
	for (int i = 0; i < n; i++)
	{
		std::cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++)
		{
			std::cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	bfs(0);
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef pair< int, int > Pi;
const int INF = 1 << 30;
struct edge: vector< Pi > {
  void add_edge(int to, int cost){
    push_back(Pi(to,cost));
  }
} info[100000];
int hoge[100000];
 
void bfs(const int& st, const int& V){
  fill_n( hoge, V, INF);
  queue< int > que;
  que.push(st);
  hoge[st] = 0;
  while(!que.empty()){
    int p = que.front();
    que.pop();
    for(int i = 0 ; i < info[p].size() ; i++){
      int cost = info[p][i].second;
      int to = info[p][i].first;
      if( cost + hoge[p] < hoge[to]){
        que.push(to);
        hoge[to] = cost + hoge[p];
      }
    }
  }
}
 
int main(){
  int N;
  scanf("%d", &N);
  for(int i = 0; i < N; i++){
    int u, k;
    scanf("%d %d", &u, &k);
    u--;
    while(k--){
      int v;
      scanf("%d", &v);
      info[u].add_edge( v - 1, 1);
    }
  }
  bfs(0, N);
  for(int i = 0 ; i < N ; i++ ){
    printf("%d %d\n", i + 1, hoge[i]==INF?-1:hoge[i]);
  }
}