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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=1<<21;
const int maxn=100+10;
int n;
vector<int> g[maxn];
int vis[maxn];
int d[maxn];
void bfs(int s)
{
    queue<int> que;
    que.push(s);
    vis[s]=1;
    d[s]=0;
    while(!que.empty())
    {
        int u=que.front();que.pop();
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(!vis[v])
            {
                que.push(v);
                d[v]=min(d[v],d[u]+1);

            }
        }
    }
}
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int no,cnt,dd;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&no,&cnt);
        for(int j=0;j<cnt;j++)
        {
            scanf("%d",&dd);
            g[no-1].push_back(dd-1);
        }
    }
    for(int i=0;i<n;i++) d[i]=INF;
    bfs(0);
    for(int i=0;i<n;i++)
    {
        printf("%d %d\n",i+1,d[i]==INF?-1:d[i]);
    }

    return 0;
}

C++: #include <iostream>
using namespace std;
#define N 100
#define WHITE 0
#define BLACK 1

int M[N+1][N+1];
int n, dist;
int DIST[N+1];
int color[N+1];


//キューを使うプログラムを考える
void bfs(int r) {
    int u;
    int next[N+1], next_num = 0;
    bool added = false;
    //現時点での距離を入れておく
    for(int i=1; i<=n; i++) {
        if(M[r][i]==1 && color[i]==WHITE) {
            if(!added) {
                dist++;
                added = true;
            }
            color[i] = BLACK;
            DIST[i] = dist;
            next[next_num] = i;
            next_num++;
        }
    }
    
    for(int i=0; i<next_num; i++) {
        cout << "next is " << next[i] << endl;
        bfs(next[i]);
    }
}   

int main() {
    int u, k, v;

    cin >>n;
    for(int i=0; i<=n; i++) {
        DIST[i] = -1;
        color[i] = WHITE;
        for(int j=0; j<=n; j++) {
            M[i][j] = 0;
        }
    }

    for(int i=0; i<n; i++) {
        cin >> u >> k;
        for(int j=0; j<k; j++) {
            cin >> v;
            M[u][v] = 1;
        }
    }

    dist = 0;
    DIST[1] = 0;
    bfs(1);

    for(int i=1; i<=n; i++) {
        cout << i << " " << DIST[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>


#define ALREADY 1
#define YET 0

int *passed;
int *distance_ps;

using namespace std;


void bfs(vector< vector<int> > a, int i, int dis){
  // i?????????vector<int>??????????????¢?????????
  dis++;
  vector<int> ai = a[i];
  int n = ai.size();
  // cout << "i: " << i << ",n: " << n << endl;
  for(int j=0;j<n;j++){
    int nexti=ai[j]-1;
    
    if(passed[nexti]==YET || distance_ps[nexti]>dis){
      passed[nexti]=ALREADY;
      distance_ps[nexti]=dis;
      bfs(a, nexti, dis);
    }
  }
}

int main(){
  int n;
  cin >> n;
  vector< vector<int> > a;
  for(int i=0;i<n;i++){
    int u;
    int k;
    cin >> u >> k;
    vector<int> tmpa;
    for(int j=0;j<k;j++){
      int tmp;
      cin >> tmp;
      tmpa.push_back(tmp);
    }
    a.push_back(tmpa);
  }
  
  int a2[n][n];
  passed = new int[n];
  distance_ps = new int[n];
  
  for(int i=0;i<n;i++){
    passed[i] = 0;
    distance_ps[i] = 1000;
  }

  distance_ps[0]=0;
  bfs(a, 0, 0);
  
  for(int i=0;i<n;i++){
    cout << i << " " << distance_ps[i] << endl;
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      a2[i][j]=0;
    }
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<(int)a[i].size();j++){
      a2[i][a[i][j]-1] = 1;
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      // if(j!=0)cout << " ";
      // cout << a2[i][j];
    }
    // cout << endl;
  }
      
  
}

C++: #include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GLAY 1

// 頂点の個数
int n;
// 隣接行列
vector<vector<int> > M(100, vector<int>(100));
// 頂点の深さを格納する配列
vector<int> D(100, -1);
// 頂点の状態を格納する配列
vector<int> color(100, WHITE);
// 訪問すべき頂点を格納するキュー
queue<int> Q;

void bfs()
{
  // 初期化
  Q.push(0);
  D.at(0) = 0;
  while (!Q.empty())
  {
    // 未訪問の頂点が残っているとき
    int u = Q.front();
    Q.pop();
    color.at(u) = GLAY;
    for (int v = 0; v < n; v++)
    {
      if (M.at(u).at(v) == 1 && color.at(v) == WHITE && D.at(v) == -1)
      {
        // vはuから直接訪問でき、かつ未訪問の頂点、かつ深さ未設定のとき
        Q.push(v);
        D.at(v) = D.at(u) + 1;
      }
    }
  }
}

int main()
{
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    int u, k;
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++)
    {
      int v;
      cin >> v;
      v--;
      M.at(u).at(v) = 1;
    }
  }

  bfs();

  for (int i = 0; i < n; i++)
  {
    cout << i + 1 << " " << D.at(i) << endl;
  }
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(const vector<vector<int>>& graph_)
      : graph(graph_), dist(graph.size(), -1) {}

  void solve() {
    q.push(make_pair(0, 0));
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      auto v = p.first;
      auto d = p.second;
      if (dist[v] != -1 && dist[v] <= d) {
        continue;
      }
      dist[v] = d;
      for (auto& v2 : graph[v]) {
        if (dist[v2] == -1 || dist[v2] > d + 1) {
          q.push(make_pair(v2, d + 1));
        }
      }
    }
  }

  const vector<vector<int>>& graph;
  vector<int> dist;
  queue<pair<int, int>> q;
};

int main() {
  int n;
  cin >> n;
  vector<vector<int>> graph(n, vector<int>());
  for (int i = 0; i < n; ++i) {
    int u;
    int k;
    cin >> u >> k;
    graph[u - 1].reserve(k);
    for (int j = 0; j < k; ++j) {
      int v;
      cin >> v;
      graph[u - 1].push_back(v - 1);
    }
  }

  Solver s(graph);
  s.solve();
  for (size_t i = 0; i < s.dist.size(); ++i) {
    cout << (i + 1) << " " << s.dist[i] << endl;
  }
}