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++: #include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <bitset>
#include <cstdio>
#include <set>
#include <stack>
#include <queue>
#include <cassert>
#include <numeric>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
    REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}
#define vi vector<int>
#define vii vector<vector<int>>
#define mii map<int,int>
#define Sort(v) sort(v.begin(),v.end())
#define Reverse(v) reverse(v.begin(),v.end())
#define ALL(a)  (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
//#define int ll

const int md = 1000000007;
const int INF = 1<<30;



int main(){
    int n; cin >> n;
    vector<vector<int>> e(n);


    rep(i,n){
        int s; cin >> s;
        int tmp; cin >> tmp;
        rep(j,tmp){
            int to; cin >> to;
            e[i].push_back(to-1);
        }
    }

    vi ans(n);
    rep(i,n){
        //if(i != 3) continue;
        queue<pair<int,int>> bfs;
        bfs.emplace(0,0);
        while(!bfs.empty()){
            auto tmp = bfs.front();
            if(tmp.first == i) {
                ans[i] = tmp.second;
                break;
            }
            bfs.pop();

            rep(j,e[tmp.first].size()){
                bfs.emplace(e[tmp.first][j],tmp.second+1);
            }
        }

        ans[i] = -1;
    }

    rep(i,n){
        cout << i+1 << " " << ans[i] << endl;
    }
}

C++: #include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cctype>
#include <bits/stdc++.h>
#include <queue>

using namespace std;
// for loop
#define F(i, L, R) for(long i = L; i < R; i++)
#define FE(i, L, R) for(long i = L; i <= R; i++)
// for (reversed)
#define FR(i, L, R) for(long i = L; i > R; i--)
#define FRE(i, L, R) for(long i = L; i >= R; i--)

// data types
#define Int long
#define ul unsigned long
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned shoft

// vectors
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define ALL(c) (c).begin(),(c).end()
#define last(vec) vec.size()-1
#define lastEle(vec) vec[last(vec)]
#define pb push_back
#define InputV(a, n) F(i,0,n){long l; cin >> l; a.pb(l);}

// debug
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i==R?'\n':' ')
#define printAF(a, L) printA(a, 0, L-1)
#define printV(a) printA(a, 0, a.size()-1)

// utils
#define char2int(c) (c-'0')
#define bit(x, i) (x&(1<<i))
#define space ' '
#define PI 3.14159265358979323

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

void printM(vector<vector<int> > &vec, long height, long width){
    F(i, 0, height){
        F(j, 0, width){
            cout << vec[i][j] << space;
        }
        cout << endl;
    }
}

vector<int> seen;
vector<int> done;
vector<int> cost;
int timestamp;

// Graph: グラフを表した隣接リスト
// v: 何番目のノードを訪問するのか
void dfs(vector<vector<int> > &Graph, int v){
    timestamp++;
    seen[v] = timestamp; //訪問済みにする

    // 隣接するノードを再帰的に訪問
    for(int next_v = 0; next_v < Graph[v].size();next_v++) {
        if(seen[Graph[v][next_v]] != -1) continue;
        dfs(Graph, Graph[v][next_v]);
    }
    // 出るときの時間が増える
    timestamp++;
    done[v] = timestamp; // 隣接ノードを全て調べ終えた
}

queue<int> que;
vector<long> dist;
// v: 何番目の頂点を訪問しているのか
void bfd(vector<vector<int> > &Graph, int v){
    done[v] = timestamp;

    for(int next_v = 0; next_v < Graph[v].size();next_v++){
        if(seen[Graph[v][next_v]] == -1){
            dist[Graph[v][next_v]] = dist[v] + 1;
            seen[Graph[v][next_v]] = timestamp;
            que.push(Graph[v][next_v]);
        }else{
            dist[Graph[v][next_v]] = min(dist[Graph[v][next_v]], dist[v] + 1);
        }
    }
}

#define TST 0
int main() {
#if TST==1
    ifstream in ("input.txt");
    cin.rdbuf(in.rdbuf());
#endif
    int n;
    cin >> n;
    vvi Graph;
    Graph.resize(n);
    F(i, 0, n){
        int id;
        cin >> id;
        int k;
        cin >> k;
        vi tmp;
        tmp.resize(k);
        F(j, 0, k){
           cin >> tmp[j]; 
           tmp[j]--;
        }
        Graph[i] = tmp;
    }
    seen.assign(n, -1);
    done.assign(n, -1);
    dist.assign(n, -1);
    que.push(0);
    seen[0] = 0;
    dist[0] = 0;
    while(!que.empty()){
       timestamp++;
       int front = que.front();
       bfd(Graph, front);
       que.pop();
    }

    F(i, 0, n){
        cout << i+1 << space << dist[i] << endl;;
    }

#if TST==1
#endif
}