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

const int MAX_N = 100;
int to[MAX_N][MAX_N];
int ans[MAX_N];
bool visited[MAX_N];

int main() {
  int N;
  cin >> N;
  for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
      to[i][j] = -1;
    }
    ans[i] = -1;
    visited[i] = false;
  }
  for (int i=0; i<N; i++) {
    int tmp, n;
    cin >> tmp >> n;
    for (int j=0; j<n; j++) {
      int t;
      cin >> t; t--;
      to[i][j] = t;
    }
  }

  queue<pair<int, int> > que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    pair<int, int> tmp = que.front(); que.pop();
    int x = tmp.first, depth = tmp.second;
    if (N < depth) continue;
    if (ans[x] == -1) {
      ans[x] = depth;
    } else {
      ans[x] = min(ans[x], depth);
    }
    for (int i=0; to[x][i] != -1; i++) {
      que.push(make_pair(to[x][i], depth + 1));
    }
  }

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

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

int main(int argc,char* argv[]){
  int dis[101],flag[101];
  vector<queue<int> > vec(101);
  int n,id,cnt,x,val=0;

  memset(dis,255,sizeof(dis));
  memset(flag,0,sizeof(flag));

  cin>>n;
  for(int i=0;i<n;i++){
    cin>>id>>cnt;
    queue<int> tmp;
    for(int l=0;l<cnt;l++){
      cin>>x;
      tmp.push(x);
    }
    vec[id]=tmp;
  }

  queue<int> que;
  que.push(1);
  while(!que.empty()){
    queue<int> tmp=que;
    while(!que.empty())
      que.pop();
    int p;

    while(!tmp.empty()){
      p=tmp.front();tmp.pop();
      if(dis[p]==-1)
	dis[p]=val;

      while(!vec[p].empty()){
	que.push(vec[p].front());
	vec[p].pop();
      }
    }
    val++;
  }

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

  return 0;
}

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

vector<int> G[MAX];

int bfs(int id){
	queue<int> Q;
	int temp[MAX] = {}, ans = -1;
	
	Q.push(1);
	
	while(!Q.empty()){
		int n = Q.front(); Q.pop();
		if(n == id){
			ans = temp[n];
			break;
		}
		for(int y : G[n]){
			if(temp[y]==0&&y!=1){
				temp[y] += temp[n] + 1;
				Q.push(y);
			}
		}
	}
	return ans;
}

int main(){
	int n, u, k, v, A[MAX][MAX] = {};
	
	cin>>n;
	
	for(int i = 0; i < n; i++){
		cin>>u>>k;
		for(int j = 0; j < k; j++){
			cin>>v;
			G[u].push_back(v);
		}
	}
	
	for(int i = 1; i <= n; i++){
		cout<<i<<" "<<bfs(i)<<endl;
	}
	
	
	return 0;
}

C++: #include "bits/stdc++.h"
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i)
#define ALL(v) v.begin(), v.end()
#define FS first
#define SC second
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define sqr(x) ((x) * (x))
#define bit(n) (1LL<<(n))
#define pcnt(x) __builtin_popcountll(x)
#define PB push_back
#define MP make_pair
#define endl "\n"
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<bool> VB;
typedef vector<vector<int> > VVI;
typedef vector<vector<bool> > VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const LL MOD=1000000007ll;
const LL INF=2000000000ll;

long power(long a,long b){return b?power(a*a%MOD,b/2)*(b%2?a:1)%MOD:1;}

long perm(int N, int K){
	long x=1;
	for(int i=1;i<=K;i++)
	{
		x=x*(N-i+1)%MOD;
	}
	return x%MOD;
}

long factorial(int N){
	return perm(N,N);
}

long comb(int N,int K){
	long x=1,y=1;
	for(int i=1;i<=K;i++)
	{
		x=x*(N-i+1)%MOD;
		y=y*i%MOD;
	}
	return x*power(y,MOD-2)%MOD;
}

long hcomb(int N, int K){
    return comb(N+K-1, N-1);
}

void dfs(const vector<vector<int> > &G, int v, vector<int> &seen) {
    seen[v] = true; // v を訪問済にする

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue; // next_v が探索済だったらスルー
        dfs(G, next_v, seen); // 再帰的に探索
    }
}

void wfs(const vector<vector<int> > &G, int v, vector<int> &dist){
    REP(i,SZ(dist)) dist[i] = -1; // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[v] = 0;
    que.push(v); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int next_v : G[v]) {
            if (dist[next_v] != -1) continue; // すでに発見済みの頂点は探索しない

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[next_v] = dist[v] + 1;
            que.push(next_v);
        }
    }
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);

	int n, u, k;
	cin >> n;
	VVI G(n);
	REP(i,n){
		cin >> u >> k;
		if(k){
			G[i].resize(k);
			REP(j,k){
				cin >> G[i][j];
				G[i][j]--;
			}
		}
	}

	VI d(n);
	wfs(G,0,d);
	REP(i,n) cout << i+1 << " " << d[i] << endl;

}