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 "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T>
inline vector<T> make_v(const T &initvalue, size_t sz) {
    return vector<T>(sz, initvalue);
}

template <class T, class... Args>
inline auto make_v(const T &initvalue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initvalue, args...))>(sz, make_v<T>(initvalue, args...));
}

constexpr int INF = 0x3f3f3f3f;
constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
// }}} End

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    size_t N; cin >> N;

    vector<vector<size_t>> g(N);
    for (size_t i = 0; i < N; ++i) {
        size_t u, k; cin >> u >> k;
        u--;
        for (size_t j = 0; j < k; ++j) {
            size_t v; cin >> v;
            v--;
            g[u].emplace_back(v);
        }
    }

    vector<int> dist(N, INF);
    dist[0] = 0;

    queue<size_t> que;
    que.emplace(0);

    while (!que.empty()) {
        size_t now_node = que.front();
        que.pop();
        for (auto &next_node: g[now_node]) {
            if (chmin(dist[next_node], dist[now_node] + 1)) {
                que.emplace(next_node);
            }
        }
    }

    for (size_t idx = 0; idx < N; ++idx) {
        cout << idx + 1 << " " << (dist[idx] == INF ? -1 : dist[idx]) << "\n";
    }

    return 0;
}

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

int main() {
	int a[101][101] = {{0}};
	int u, k, v, km;
	km = 0;
	while ( cin >> u, k ) {
		for ( int i = 1; i <= k; i++ ) {
			cin >> v;
			a[u][v] = 1;
		}
		km++;
	}
	int n0;
	int b[101] = {- 1};
	queue<int> q;
	q.push( 1 );
	b[1] = 0;
	int cou = 0;
	for ( int i = 1; i <= km; i++ ) {
		n0 = q.front();
		q.pop();
		cou++;
		for ( int j = 1; j <= km + 1; j++ ) {
			if ( a[n0][j] == 1 && b[j] == - 1 ) {
				q.push( j );
				b[j] = cou;
			}
			if ( q.empty ) break;
		}
	}
	for ( int i = 1; i <= km; i++ )
		cout << i << " " << b[i] << endl;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); i++) 
#define FOR(i, a, b) for(int (i) = a; (i) < (int)b; i++)
#define RREP(i, n) for(int (i)=((int)(n)-1); (i)>=0; i--)
#define RFOR(i, a, b) for(int (i) =((int)(b)-1); (i)>=(int)a; i--)
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define NIL -1
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define SZ(x)  (int)x.size()
#define SP(x)  setprecision((int)x)


using namespace std ;
typedef long long ll;
using Graph = vector<vector<int>>;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<string> vstr;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} //最小公倍数

//-------------------------------------------------
// 
void yes(){
  cout <<"Yes"<<endl ;
}
void no(){
  cout <<"No"<<endl ;
}


//-------------------------------------------------
// メモ
/*






*/
//-------------------------------------------------


int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n ;
  cin >> n ;
  vint d(n,-1) ;
  vint seen(n,0) ;
  queue<int> Q ;
  vvint gra(n) ;
  for(int i=0;i<n;i++){
    int a, b;
    cin >> a >> b;
    for(int j=0;j<b;j++){
      int c ;
      cin >> c ;
      gra[a-1].PB(c-1) ;
    }
  }
  
  Q.push(0) ;
  seen[0] = 1 ;
  d[0] = 0 ;
  
  while(!Q.empty()){
    int k = Q.front() ;
    for(int i=0;i<SZ(gra[k]);i++){
      if(seen[gra[k][i]]==0){
        Q.push(gra[k][i]) ;
        seen[gra[k][i]] = 1 ;
        d[gra[k][i]] = d[k] + 1 ;
      }
    }
    Q.pop() ;
  }
  
  for(int i=0;i<n;i++){
    cout << i+1 <<" "<<d[i] <<endl ;
  }
 
  return 0 ;
}

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

int *d;
int *k;
int **a;

void bfs(){
    int i, v, u;
    queue<int> q;
    q.push(1);
    d[1] = 0;
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(i=0;i<k[u];i++){
            v = a[u][i];
            if(d[v]<0){
                q.push(v);
                d[v] = d[u] + 1;
            }
        }
    }
}

int main(){
    int i, j, n, u, v;
    scanf("%d",&n);
    d = (int *)malloc(sizeof(int)*(n+1));
    k = (int *)malloc(sizeof(int)*(n+1));
    a = (int **)malloc(sizeof(int *)*(n+1));
    for(i=0;i<=n;i++)d[i] = -1;

    for(i=0;i<n;i++){
        scanf("%d",&u);
        scanf("%d",k+u);
        a[u] = (int *)malloc(sizeof(int)*k[u]);
        for(j=0;j<k[u];j++){
            scanf("%d",&v);
            a[u][j] = v;
        }
    }

    bfs();

    for(i=1;i<=n;i++)printf("%d %d\n",i,d[i]);

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 100
#define Black 1
#define White 0

int f[MAX];
int adj[MAX][MAX];
int n;

void bfs(int r){
    rep(i,n)f[i]=-1;
    queue<int> Q;
    Q.push(r);
    f[r]=0;
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        rep(i,n){
            if(adj[u][i]==1&&f[i]==-1){
                f[i]=f[u]+1;
                Q.push(i);
            }
        }
    }
    rep(i,n){
        printf("%d %d\n",i+1,f[i]);
    }
}

int main(){
    int id,k,node;
    cin>>n;
    rep(i,n)rep(j,n)adj[i][j]=0;
    rep(i,n){
        scanf("%d %d",&id,&k);
        id--;
        rep(j,k){
            scanf("%d",&node);
            node--;
            adj[id][node]=1;
        }
    }
    bfs(0);
    return 0;
}