Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <stack>
#include <map> 
#include <set>
#include <string>
#include <functional>
#include <list>
#include <random>
#include <time.h>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <new>
#include <sstream>
#include <complex>
#define BIT(nr) (1ULL << (nr))
#define int long long
#define ll long long
#define double long double
#define mod 1000000007
#define MAXN (int)1e+5 * 2+1
#define LL_MAX 9223372036854775807	
#define LL_HALFMAX 9223372036854775807 / 2	
#define MIN -(9223372036854775807 / 2)
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
template<typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

using namespace std;

using Weight = int;
using Flow = int;
struct Edge {
	int src, dst;

	// libalgo のものに追加、メンバを追加するだけなので互換性は崩さないはず、逆辺のG[e.dstの]インデックスを保持
	int rev;
	Weight weight;
	Flow cap;
	Edge() : src(0), dst(0), weight(0) {}
	Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
	bool operator<(const Edge& right) const {
		return dst < right.dst;
	}
};

using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph& g, int a, int b, Weight w = 1) {
	g[a].emplace_back(a, b, w);
	g[b].emplace_back(b, a, w);
}
void add_arc(Graph& g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }

int dx[4] = { 0, 1, 0, -1 }; // x軸方向への変位
int dy[4] = { 1, 0, -1, 0 }; // y軸方向への変位

signed main() {
	int n;
	cin >> n;
	vector<int> dist(n + 1, -1);

	Graph G(n + 1);
	rep(i, n) {

		int u, k;
		cin >> u >> k;
		rep(j, k) {
			int v;
			cin >> v;
			add_arc(G, u, v);
		}
	}

	queue<int> q;
	q.push(1);
	dist[1] = 0;
	while (!q.empty())
	{
		int node = q.front();
		q.pop();
		for (Edge e : G[node]) {
			if (dist[e.dst] == -1) {
				dist[e.dst] = dist[e.src] + 1;
				q.push(e.dst);
			}
		}
	}

	REPS(i, n) {
		cout << i << " " << dist[i] << "\n";
	}

	return 0;
}

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

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

	std::vector<std::vector<int>> edges(n);
	for (auto&& v : edges) {
		int k;
		std::cin >> k >> k;

		for (int i = 0; i < k; ++i) {
			int u;
			std::cin >> u;
			--u;

			v.push_back(u);
		}
	}

	std::vector<int> depth(n, -1);
	depth[0] = 0;

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

		for (auto&& dist : edges[cur]) {
			if (depth[dist] == -1) {
				depth[dist] = depth[cur] + 1;
				que.push(dist);
			}
		}
	}

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

C++: #include <iostream>
#include <stack>
#include <queue>

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    stack<int> *q = new stack<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(!q->empty())
    {
        int u = q->top();
        q->pop();

        for (int v = 0; v < N; v++)
        {
            if (M[u][v] && C[v] == WHITE)
            {
                C[v] = GRAY;
                D[v] = D[u] + 1;
                q->push(v);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

        for (int j = 0; j < N; j++)
        {
            M[i][j] = 0;
        }
    }

    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--;
            M[u][v] = 1;
        }
    }

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

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

    return 0;
}

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

int D[100];
int a[100][100];

int main() {
  for (int i = 0; i < 100; i++){
    for (int j = 0; j < 100; j++){
	  a[i][j] = 0;
	  }
	 }
	int n,u,k,v;
	cin >> n;
	for (int i = 0; i < n; i++) {
	  cin >> u >> k;
	  for (int j = 0; j < k; j++) {
	    cin >> v;
		a[u][v] = 1;
		}
	  }
queue<int> Q; //整数を管理するキューの定義
  for (int i = 1; i < n+1; i++){
   D[i] = -1;
   }
  int src = 1;
  Q.push(src);
  D[src] = 0; //出発点
  while (! Q.empty()) {
    int cur = Q.front(); //先頭要素を取り出す
	Q.pop();
	//動作確認用表示
	cerr << "visiting " << cur << ' ' << D[cur] << endl;
	for(int dst = 1 ; dst <= n; dst++) { //各行き先dstに対して
	if((a[cur][dst] == 1 && D[dst] == -1)) { //curからdstに辺があり、dstが未訪問なら
	      D[dst] = D[cur] + 1; //
		    Q.push(dst); //dstを訪問先に入れる
			}
		  }
		}
	for (int i=1; i < n+1; i++){
	  cout << i << " " << D[i] << endl;
	  }
	}