Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

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

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

queue<int> Q;
int n,u,k,v,tem;
int D[110];
vector<int> edges[110];
void solve() {
    D[1] = 0;
    while (! Q.empty()) {
        int cur = Q.front();
        Q.pop();
        while(edges[cur].size()){
            tem=edges[cur].back();
            edges[cur].pop_back();
            if(D[tem]==-1){
                Q.push(tem);
                D[tem]=D[cur]+1;
            }
        }
    }
        for (int i=1;i<=n;i++) {
            cout<<i<<' '<<D[i]<<endl;
        }
}


int main(){
    for(int i=0;i<110;i++){
        D[i]=-1;
    }
    cin>>n;
    for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
        cin>>v;
        edges[u].push_back(v);
    }
}
    Q.push(1);
    solve();
        return 0;
}