Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

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

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

C++: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
ll dx[4] = { 0, 1, 0, -1 };
ll dy[4] = { 1, 0, -1, 0 };
const ll INF = 1 << 21;
const ll MOD = 1e9 + 7;
static const int N = 100;

int n;
int M[N][N];
int d[N]; // 距離で訪問状態を管理する

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    rep(i, n)
    {
        d[i] = INF;
    }
    d[s] = 0;
    int u;
    while (!q.empty()) {
        u = q.front();
        q.pop();
        rep(v, n)
        {
            if (M[u][v] == 0) { // u->vに行けないなら飛ばす
                continue;
            }
            if (d[v] != INF) { // d[v]に行ったことがあるなら飛ばす
                continue;
            }
            d[v] = d[u] + 1; // u->vが始めてなら距離を更新
            q.push(v); // queueにvを入れる
        }
    }
    rep(i, n)
    {
        cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int u, k, v;
    cin >> n;
    rep(i, n)
    {
        rep(j, n)
        {
            M[i][j] = 0;
        }
    }
    rep(i, n)
    {
        cin >> u >> k;
        u--;
        rep(j, k)
        {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0); // 0から出発

    return 0;
}

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

int n,m,k,l,a[101][101],d[101],t;
queue<int> q;

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>l>>m;
        for(int j=0;j<m;j++){
            cin>>k;
            a[l][k]=1;
        }
        d[i+1]=-1;
    }
    d[1]=0;
    q.push(1);
    while(!q.empty()){
        t=q.front();
        q.pop();
        for(int i=1;i<=n;i++){
            if(a[t][i]){
                if(d[i]==-1){
                    d[i]=d[t]+1;
                    q.push(i);
                }
            }
        }
    }
    for(int i=1;i<=n;i++){cout<<i<<' '<<d[i]<<endl;}
}

C++: #include <bits/stdc++.h>
//#define int long long
#define double long double
#define rep(i, a, n) for(int i = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int i = (int)(a);(i) <= (int)(n);++(i))
#define repif(i, a, x) for(int i = (int)(a);(x);++(i))
#define repr(i, a, n) for(int i = ((int)(a)-1);(i) >= (int)(n);--(i))
#define reper(i, a, n) for(int i = (int)(a);(i) >= (int)(n);--(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), end(x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x)/gcd(x,y)*(y))
#define isin(x, l, r) ((l) <= (x) and (x) < (r))
#define fcout(n) cout << fixed << setprecision(n)
#define rcout(n) cout << right << setw(n)
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define PI (acosl(-1))
#define EPS (1e-10)
#define INF LLONG_MAX
#define INF32 INT32_MAX
#define MOD 1000000007LL
using namespace std;
using VI = vector<int>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<PII>;
using VTIII = vector<TIII>;
using Complex = complex<double>;
template<typename T, template<typename> typename C = less> using PQ = priority_queue<T, vector<T>, C<T>>;
template<typename T> inline bool chmax(T& a, const T& b, bool t = false){ if(a<b or (t and a == b)){ a=b; return true; } return false; }
template<typename T> inline bool chmin(T& a, const T& b, bool t = false){ if(a>b or (t and a == b)){ a=b; return true; } return false; }


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


signed main(){
    cin >> n;
    rep(i, 0, n){
        int u, k;
        cin >> u >> k;
        u--;
        int v;
        rep(j, 0, k){
            cin >> v;
            g[u].pb(v-1);
        }
    }

    fill(d, d+n, -1);
    queue<int> q;
    d[0] = 0;
    q.push(0);
    while(!q.empty()){
        int p = q.front(); q.pop();
        rep(i, 0, SZ(g[p])) if(d[g[p][i]] == -1) d[g[p][i]] = d[p]+1, q.push(g[p][i]);
    }

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

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef vector <int> vi;
typedef vector <vi> vvi;

int addedge(vvi &graph, int y, int x, int val=1){
	graph[y][x] = val;
	return val;
}

int make_graph(vvi &graph, vi &stamp){
	int n1=graph.size(), n2, row, col;

	for (int j=0; j<n1; j++){
		cin>>row;
		cin>>n2;
		for (int i=0; i<n2; i++){
			cin>>col;
			addedge(graph, row-1, col-1);
		}
		stamp[j] = -1;
	}
	return 0;
}

int Breadth_First_Search(vvi &graph, vi &stamp){

	queue <int>qu;
	qu.push(0);
	stamp[0] = 0;
	while (! qu.empty()){
		int tmp = qu.front();
		qu.pop();
		for (int i=0; i<(int)graph[0].size(); i++){
			if (graph[tmp][i]==1 && stamp[i]==-1){
				stamp[i] = stamp[tmp]+1;
				qu.push(i);
			}
		}
	}
	return 0;
}

int resultout(vi &stamp){
	for (int i=0; i<int(stamp.size()); i++){
		cout<<i+1<<" "<<stamp[i]<<endl;
	}
	return 0;
}

int main(void){

	int n, row;
	cin>>n;
	vvi G(n, vi(n));
	vi stamp(n);

	make_graph(G, stamp);

	Breadth_First_Search(G, stamp);

	resultout(stamp);

	return 0;
}