Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

int main(){
  vector<int> ans;
  vector<vector<int> > path_field;
  int n;

  cin >> n;
  n++;
  path_field.resize(n);
  ans.resize(n);
  for(int i=1;i<n;i++){
    ans[i]=-1;
  }
  for(int i=1;i<n;i++){
    int _m,m;

    path_field[i].resize(n);
    for(int j=1;j<n;j++){
      path_field[i][j]=0;
    }

    cin >> _m >> m;
    for(int j=0;j<m;j++){
      int to;
      
      cin >> to;
      path_field[i][to]=1;
    }
  }

  deque<tuple<int,int> > que;

  que.push_back(make_tuple(1,0));
  ans[1]=0;
  while(!que.empty()){
    int& standingby=get<0>(que.front());

    for(int i=1;i<n;i++){
      if((path_field[standingby][i]==1)&&(ans[i]==-1)){
        que.push_back(make_tuple(i,get<1>(que.front())+1));
	ans[i]=get<1>(que.front())+1;
      }
    }
    que.pop_front();
  }

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

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

int main(){
  int n;
  cin>>n;
  vector<vector<int>> g(n);
  for(int i=0;i<n;i++){
   int I,k;
    cin>>I>>k;
    for(int j=0;j<k;j++){
      int x;
      cin>>x;
      x--;
      g[i].push_back(x);
    }
  }
  
  	vector<int> d(n,-1);
 	 queue<int> q;
  
 	 d[0]=0;
 	 q.push(0);
  
 	 while(!q.empty()){
  	  int v=q.front(); q.pop();
     
  	  for(int &nv: g[v]){
    	  if(d[nv]!=-1) continue;
       
    	  d[nv]=d[v]+1;
    	  q.push(nv);
      }
     }
  for(int i=0;i<n;i++) cout<<i+1<<" "<<d[i]<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, u, v, k, l;
int edge[10001];
int start[101];
int goal[101];
int c;
int bfs[101];
queue<int> Q;
int main()
{
	cin >> n;
	c=0;
	for(int i=1; i<=n; i++)
	{
		cin >> u;
		start[i]=c;
		cin >> k;
		c+=k;
		goal[i]=c;
		for(int j=start[i]; j<goal[i]; j++)
		{
			cin >> edge[j];
		}
	}
	for(int i=1; i<=n; i++)
	{
		bfs[i]=-1;
	}
	bfs[1]=0;
	Q.push(1);
	while(!Q.empty())
	{
		v=Q.front();
		Q.pop();
		for(int j=start[v]; j<goal[v]; j++)
		{
			l=edge[j];
			if(bfs[l]==-1)
			{
				bfs[l]=bfs[v]+1;
				Q.push(l);
			}
		}
	}
	for(int i=1; i<=n; i++)
	{
		cout << i << ' ' << bfs[i] << endl;
	}
	return 0;
}

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

int n;
vector<int> d;
vector<vector<int>> graph;
vector<bool> bfsflg(n, 0);
void bfs(int z){
    queue<int> q;
    bfsflg[z] = 1;
    d[1]=0;
    q.push(z);

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

        for(int i : graph[v]){
            if(!bfsflg[i]){
                bfsflg[i] = 1;
                d[i]=d[v]+1;
                q.push(i);
            }
        }
    }
}

int main() {
    cin >> n;
    d.assign(n+1, -1);
    graph.assign(n+1, {});
    bfsflg.assign(n+1, 0);
    int u, k, tmp;
    for(int i=1; i<=n; i++){
        cin >> u >> k;
        for(int j=0; j<k; j++){
            cin >> tmp;
            graph[i].push_back(tmp);
        }
    }
    
    bfs(1);

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

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  int m[n];
  int v[n];
  rep(i,n){
    m[i]=10000;
    v[i]=0;
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
        d[i][j]=10000;
      }
    }
  }
  m[0]=0;

  rep(i,n){
    int u,k;
    cin >> u >> k;
    if(k>0){
      rep(j,k){
        int v;
        cin >> v;
        if(d[v-1][u-1]!=1){
          d[u-1][v-1]=1;
        }
      }
    }
  }   

  q1.push(0);
  while(!q1.empty()){
    int u = q1.front(); q1.pop();
      rep(j,n){
        if(d[u][j]==1 && v[j]==0){
          q1.push(j);
          m[j] = min(m[j],m[u]+1);
        }
      }
    v[u]=1;
  }

  rep(i,n){
    cout << i+1 << ' ';
    if(m[i]==10000){
      cout << -1 << endl;
    }else{
      cout << m[i] << endl;
    }
  }

  return 0;
}

C++: #include <vector>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

int n, u, k, v, dp[101];
bool li[101][101];


int main(){
	fill(dp, dp + 101, -1);
	for (int i = 0; i < 101; i++) {
		fill(li[i], li[i] + 101, false);
	}
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			li[u][v] = li[v][u] = true;
		}
	}
	vector<int> l = vector<int>(1, 1);
	vector<int> l2 = vector<int>();
	dp[1] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < l.size(); j++) {
			for (int k = 1; k <= n; k++) {
				if (li[l[j]][k] && dp[k] < 0) {
					dp[k] = i;
					l2.push_back(k);
				}
			}
		}
		l2.swap(l);
		vector<int>().swap(l2);
	}
	for (int i = 1; i <= n; i++) {
		cout << i << " " << dp[i] << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define PI 3.14159265358979323846
#define vll vector<long long>
#define vvll vector<vector<long long>>
using namespace std;
using ll = long long;
using ld = long double;

// ----本問について----
// 問題：Breadth First Search
// 問題文URL：https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C
// 所要時間：
// 学んだこと：
// ------------------

// ----ここから本問のソースコード----
static const ll N_MAX = 110;

// グローバル変数（入力）
ll n, A[N_MAX][N_MAX];

// 入力
void input() { cin >> n; }

// 幅優先探索
void bfs() {
  queue<ll> q;

  // 距離を-1で初期化
  vll d(n + 1, -1);

  // 始点の処理
  d[1] = 0;
  q.push(1);

  // 探索
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    rep(i, n) {
      if (A[v][i + 1] == 1 && d[i + 1] == -1) {
        q.push(i + 1);
        d[i + 1] = d[v] + 1;
      }
    }
  }
  rep(i, n) { cout << i + 1 << " " << d[i + 1] << endl; }
}

void solve() {
  // 隣接リストを隣接行列に変換して二次元配列Aに格納
  rep(i, n) {
    ll v, k;
    cin >> v >> k;
    rep(j, k) {
      ll v2;
      cin >> v2;
      A[v][v2] = 1;
    }
  }

  bfs();
}

int main() {
  input();
  solve();
  return 0;
}