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 <cstdint>
#include <queue>
#include <vector>

using namespace std;

#define GRAPH_SIZE 101
#define START_INDEX 1

enum color_t {
  WHITE,
  GRAY,
  BLACK,
};

struct data_t {
  uint32_t index;
  vector<uint32_t> connection;
  uint32_t distance;
};

static vector<uint32_t> graph[GRAPH_SIZE];
static color_t state[GRAPH_SIZE];

void breadth_first_search(void (*callback)(uint32_t, uint32_t))
{
  for (uint32_t i = START_INDEX; i < GRAPH_SIZE; i++) {
    state[i] = WHITE;
  }

  queue<data_t> queue;

  data_t data;
  data.index = START_INDEX;
  data.connection = graph[START_INDEX];
  data.distance = 0;

  queue.push(data);
  state[START_INDEX] = GRAY;

  while (!queue.empty()) {
    data_t top = queue.front();
    queue.pop();

    state[top.index] = BLACK;
    callback(top.index, top.distance);

    for (uint32_t i = 0; i < top.connection.size(); i++) {
      if (state[top.connection[i]] == WHITE) {
        data_t data;
        data.index = top.connection[i];
        data.connection = graph[top.connection[i]];
        data.distance = top.distance + 1;
        queue.push(data);
        state[data.index] = GRAY;
      }
    }
  }
}

#include <iostream>

int32_t distances[GRAPH_SIZE];

static void callback(uint32_t index, uint32_t distance)
{
  distances[index] = distance;
}

int32_t main()
{
  uint32_t num;
  cin >> num;

  for (uint32_t i = START_INDEX; i < START_INDEX + num; i++) {
    distances[i] = -1;
  }

  for (uint32_t i = 0; i < num; i++) {
    uint32_t index, len;
    cin >> index >> len;
    for (uint32_t j = 0; j < len; j++) {
      uint32_t val;
      cin >> val;
      graph[index].push_back(val);
    }
  }

  breadth_first_search(callback);

  for (uint32_t i = START_INDEX; i < START_INDEX + num; i++) {
    cout << i << " " << distances[i] << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define sortr(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define max 100005
#define nil -1

constexpr int inf = 1000000;
vector<vector<int>> g;
vector<int> d;

void bfs(int s){
    d.resize(max);
   fill(d.begin(),d.end(),inf);
   d[s]=0;
   queue<int> q;
   q.push(s);
   while(!q.empty()){
       int now=q.front();
       q.pop();
       for(auto &e:g[now]){
           if(d[e]==inf){
               d[e]=d[now]+1;
               q.push(e);
           }
       }
   }
}

int main() {
  int  n,u,k,v;
  cin>>n;
    g.resize(n);
    d.resize(n);
  rep(i,n){
      cin>>u>>k;
      u--;
      for(int j=0;j<k;j++){
          cin>>v;
          v--;
          g[u].push_back(v);
        }
    }
    bfs(0);
    rep(i,n){
        if(d[i]<inf){
            cout<<i+1<<" "<<d[i]<<endl;
        }else{
            cout<<i+1<<" "<<-1<<endl;
        }
    }
  return 0;
}

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

using namespace std;
using Graph = vector<vector<int>>;

int main() {
	int n;
	cin >> n;

	Graph G(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--;
			G[u].push_back(v);
		}
	}

	vector<int> dist(n, -1);
	queue<int> q;
	q.push(0);
	dist[0] = 0;

	while (!q.empty()) {
		int v = q.front();
		q.pop();

		for (int nv : G[v]) {
			if (dist[nv] != -1) continue;
			dist[nv] = dist[v] + 1;
			q.push(nv);
		}
	}

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

	return 0;
}

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

static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i ++) d[i] = INFTY;
  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front(); q.pop();
    for (int v = 0; v < n; v ++) {
      if (M[u][v] == 0) continue;
      if (d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i ++) {
    cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;

  for (int i = 0; i < n; i ++) {
    for (int j = 0; j < n; j ++) M[i][j] = 0;
  }

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

  bfs(0);
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int n=0;//頂点数 ○
int u=0;//頂点の番号　○
int k[101]={0};//頂点番号の値　○
int v[101][101]={0};//??
int d[101]={0};//shortest ○
int fptr=0;//デキューをするための変数　○
int lptr=0;//エンキューをするための変数　○
int u1[10000];//頂点の番号　○
int d1[10000];//頂点への最短距離　○
int id=0;//i==id.　○
void enqueue(int u2,int d2)
{
	u1[lptr]=u2;//頂点の番号はu2
	d1[lptr]=d2;//頂点への最短距離はd2
	lptr=(lptr+1)%10000;//今何番目の配列か求める
}
void bfs(void)
{
	int u3;
	int d3;

	while(fptr!=lptr)
	{
		u3=u1[fptr];//頂点番号代入
		d3=d1[fptr];//最短距離代入
		fptr=(fptr+1)%10000;
		if(d[u3]!=-1)
		{
			continue;
		}
		for(int i=0;i<k[u3];i++)
		{
			enqueue(v[u3][i],d3+1);
		}
		d[u3]=d3;
	}
	/*
	bfsの引数は二つ→それぞれをu1[fptr]、d1[fptr]に代入
	1.whileで全体を回す（条件付）
	2.デキューをする（whileの直後）
	3.最短距離が更新されてなければcontinue（条件付）	
	4.enqueue（for文で回す）
	*/
}
int main(void)
{	
	scanf("%d",&n);//頂点数を決める
	for(int i=0;i<n;i++)//0が頂点数になるまで
	{
		scanf("%d",&u);//各頂点の隣接リスト入力 1:u=%d...%d(n):u=%d
		scanf("%d",&k[u]);//配列ｋ[u]に値を入れる 1:k[1]=%d...k[u]=%d
		for(int j=0;j<k[u];j++)//j<k[u]のとき
		{
			scanf("%d",&v[u][j]);//配列ｖ[頂点の番号][]に値を入れる
		}
	}
	enqueue(1,0);
	for(int i=0;i<n+1;i++)//全ての値を未探索にする
	{
		d[i]=-1;//未探索の値
	}
	bfs();
	for(int i=1;i<=n;i++)
	{
	printf("%d %d\n",i,d[i]);
	}
	return 0;
}