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>
using namespace std;

#define MAX 100
#define NIL -2

int n, G_list[MAX+1];
int G[MAX+1][MAX+1];
int d[MAX+1][2];

void init(void){
  for(int i=0; i<=MAX; i++){
    d[i][0]=-1;
  }
}

void make_graph(void){
  scanf("%d", &n);

  for(int i=0; i<n; i++){
    int cnt, num, m;

    scanf("%d %d", &num, &cnt);
    G_list[i]=num;

    for(int j=0; j<cnt; j++){
      scanf("%d", &m);
      G[num][m]=1;
    }
  }
}

void check_depth(void){
  queue<int> Q;
  Q.push(G_list[0]);
  d[G_list[0]][0]=0;
  d[G_list[0]][1]=NIL;
  
  while(!Q.empty()){
    int top=Q.front();
    Q.pop();
    
    for(int j=0; j<n; j++){
      if(G[top][G_list[j]] == 1 && d[G_list[j]][1] != NIL){
	d[G_list[j]][0]=d[top][0]+1;
	d[G_list[j]][1]=NIL;
	Q.push(G_list[j]);
      }
    }
  }
}

void print(void){

  for(int i=0; i<n; i++){
    printf("%d %d\n", G_list[i], d[G_list[i]][0]);
  }
}

int main(void){

  init();
  make_graph();
  check_depth();
  print();
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
using vll=vector<ll>;
using vi=vector<int>;
using vb=vector<bool>;
using pii=pair<int,int>;

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;}

//pow(llpow,modpow)
template<class T> ll llpow(ll x,T n){ll ans=1;if(x==0)ans=0;while(n){if(n&1)ans*=x;x*=x;n>>=1;}return ans;}
long long modpow(long long a, long long n, long long mod) {long long res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}
//最大公約数
template<class T> inline T gcd(T x,T y){if(y==0)return x; else {return gcd(y,x%y);}}
//最小公倍数
template<class T> inline T lcm(T x,T y){return x/gcd(x,y)*y;}
//逆元
long long modinv(long long a, long long m) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)

long long INF = 1LL<<60;
int main( ){
    int n;
    cin>>n;
    vi conne[n];
    rep(i,0,n){
        int id;cin>>id;
        id--;
        int r; cin>>r;
        rep(j,0,r){
            int in; cin>>in; in--;
            conne[id].push_back(in);
        }
    }
    vi dist(n,-1);
    queue<pii> q;
    q.emplace(0,0);
    while(!q.empty()){
        pii n=q.front(); q.pop();
        int i=n.first; int d=n.second;
        if(dist[i]==-1)dist[i]=d;
        for(int itr:conne[i]){
            if(dist[itr]!=-1)continue;
            q.emplace(itr,d+1);
        }
    }
    rep(i,0,n)cout<<i+1<<" "<<dist[i]<<endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n,d[100];
bool graph[100][100], visited[100];
queue<int> q;


void bfs(int s){
    q.push(s);
    visited[0]=true;

    while(!q.empty()){
        int v=q.front(); q.pop();
        for(int v2=0; v2<n; v2++){
            if(graph[v][v2] && visited[v2]==false){
                d[v2]=d[v]+1; visited[v2]=true;
                q.push(v2);
            }
        }
    }
}


int main(void){
    cin >> n;

    for(int i=0; i<n; i++){
        visited[i]=false;
        d[i]=-1;
        for(int j=0; j<n; j++) graph[i][j]=false;
    }
    for(int i=0; i<n; i++){
        int v,k; cin >> v >> k;
        for(int j=0; j<k; j++){
            int w; cin >> w;
            graph[v-1][w-1]=true;
        }
    }
    d[0]=0;


    q.push(0);
    bfs(0);

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

}

C++: from collections import deque
n = int(input())
A = [0]*(n+1)
D = [-1]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
d.append(1)
D[1] = 0
DP[1] = 0
while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i)
for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp)

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
mat g;

lli u,k,v;

queue<lli> q;
vbl vis;
lli p;
vll dp;

lli solve(lli x){
    if(x == 1) return dp[x] = 0;
    if(dp[x]) return dp[x];

    lli ret = dp[x] = -1;
    for(lli i = 0;i < g[x].size();i++){
        ret = 
        solve(g[x][i]) < 0 
        ? ret 
        : ret < 0 
        ? solve(g[x][i]) + 1 
        : min(ret,solve(g[x][i])+1);
    }
    return dp[x] = ret;
}

int main(){
    cin >> n;
    g = mat(n+1);
    dp = vll(n+1);
    for(lli i = 0;i < n;i++){
        cin >> u >> k;
        for(lli j = 0;j < k;j++){
            cin >> v;
            g[v].push_back(u);
        }
    }
    for(lli i = 1;i <= n;i++){
        cout << i << " " << solve(i) << endl;
    }
    return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<int>* adj = new vector<int>[n];
	int* dist = new int[n];
	int t1, t2 ,t3;
	for (int i = 0;i < n;i++) {
		cin >> t1;
		t1--;
		cin >> t2;
		for (int i = 0;i < t2;i++) {
			cin >> t3;
			adj[t1].push_back(t3 - 1);
		}
		dist[i] = -1;
	}
	queue<pair<int, int> > q;
	q.push(make_pair(0, 0));
	dist[0] = 0;
	while (!q.empty()) {
		pair<int, int> p = q.front();
		q.pop();
		for (int i = 0;i < adj[p.second].size();i++) {
			t1 = adj[p.second][i];
			if (dist[t1] == -1){
				q.push(make_pair(p.first + 1, t1));
				dist[t1] = p.first + 1;
			}
		}
	}
	for (int i = 0;i < n;i++) {
		cout << i + 1 << " " << dist[i] << endl;
	}

}