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;
static const int N=100;
static const int INFTY=2000000000;
int n,M[N+1][N+1];
int d[N+1];
void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=1;i<=n;i++)d[i]=INFTY;
    d[s]=0;
    int u;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int v=1;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=1;i<=n;i++)
        cout<<i<<" "<<((d[i]==INFTY)?-1:d[i])<<"\n";

}

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

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

vector<int> bfs(
    int p,
    const vector<vector<int>> graph)
{
    queue<int> Q;
    vector<bool> status(graph.size());
    vector<int> distance(graph.size());
    Q.push(p);
    status[p] = true;
    while (!Q.empty())
    {  
        int k = Q.front();
        Q.pop();
        for (const auto& q : graph[k]) {
            if (!status[q]) {
                Q.push(q);
                status[q] = true;
                distance[q] = distance[k]+1;
            }
        }
    }
    for (int i = 0; i < status.size(); ++i) {
        if (status[i] == 0) {
            distance[i] = -1;
        }
    }
    return distance;
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> graph(n);
    for (auto&& g : graph) {
        int id;
        cin >> id;
        int size;
        cin >> size;
        g.reserve(size);
        for (int j = 0; j < size; ++j) {
            int k; 
            cin >> k;
            g.push_back(k-1);
        }
    }
    auto result = bfs(0, graph);
    for (int i = 0; i < result.size(); ++i) {
        cout << i+1 << " " << result[i] << endl;
    }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
#define ALL(l)  (l).begin(),(l).end()
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;

//const ll mod =  1000000007;
//const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------


int main() {
   int n;
   cin>>n;
   vi ans(n,-1);
   vi g[n];
   rep(i,n){
       int h,k;
       cin>>h>>k;
       rep(j,k){
           int t;
           cin>>t;
           //cout<<i<<" "<<t-1<<endl;
           g[i].push_back(t-1);
       }
   }
  
   ans.at(0)=0;
   queue<int> q ;
   q.push(0);
   
   while(!q.empty()){
      int from=q.front();
      q.pop();
      for(int to : g[from]){
          if(ans.at(to)!=-1)continue;
          ans.at(to)=ans.at(from)+1;
          q.push(to);
      }
   }
   rep(i,n){
       cout<<i+1<<" "<<ans.at(i)<<endl;
   }
}

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

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nFromMeTo;

  StDist(int nDist) : m_nDist(nDist), m_nFromMeTo(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();
    if (rvoDist[nx].m_nDist == nIMPOS)  continue;
    if (rvoDist[nx].m_nFromMeTo == 1 )  continue; 

    rvoDist[nx].m_nFromMeTo = 1;
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
      if (cnrvvnAdjcy[nx][ny] == 1)
      {
        int nDist = rvoDist[nx].m_nDist + 1;
        if (rvoDist[ny].m_nDist == nIMPOS)
          rvoDist[ny].m_nDist = nDist;
        else
          rvoDist[ny].m_nDist = min(rvoDist[ny].m_nDist, nDist);

        rqnBFSrch.push(ny);
      }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#define Lens 105
using namespace std;
int N;
int A[Lens][Lens];
int d[Lens];
char c[Lens];
queue<int> Q;

int main()
{
	cin >> N;
	for (int i = 1; i <= N; i++)
	{
		int k, n;
		cin >> k >> n;
		c[k] = 'w';
		for (int i = 0; i < n; i++)
		{
			int j;
			cin >> j;
			Q.push(j);
			A[k][j] = 1;
		}
	}
	int j = Q.front();
	for (int i = 1; i <= N; i++)
	{
		while (A[i][j] == 1 && c[j] == 'w')
		{
			Q.pop();
			d[j] = d[i] + 1;
			c[j] = 'g';
			if (Q.size() == 0)
				break;
			j = Q.front();
			while (c[j] == 'g')
			{
				Q.pop();
				if (Q.size() == 0)
					break;
				j = Q.front();
			}

		}

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