Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

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

using vvi = std::vector<std::vector<int>>;

std::vector<int> DistanseFrom(const vvi& edges, int from)
{
    using namespace std;
    constexpr int nil = -1;

    int nextDist {};
    const int n = edges.size();
    queue<int> currentQ;
    queue<int> nextQ;
    vector<int> distances(n, nil);

    nextQ.push(from);
    distances[from] = nextDist;
    ++nextDist;

    while (!nextQ.empty())
    {
        currentQ = nextQ;
        nextQ = {};
        
        while (!currentQ.empty())
        {
            int c = currentQ.back();
            currentQ.pop();

            for (const auto& e : edges[c])
            {
                if (distances[e] == nil)
                {
                    nextQ.push(e);
                    distances[e] = nextDist;
                }
            }
        }
        
        ++nextDist;
    }

    return distances;
}

void solve()
{
    using namespace std;

    int n;
    cin >> n;

    //隣接リスト形式(1オリジン)
    vvi edges(n + 1);

    //読み込み
    for (int i = 1; i <= n; ++i)
    {
        int u, k;
        cin >> u >> k;

        edges[u] = vector<int>(k);

        for (int j = 0; j < k; ++j)
        {
            cin >> edges[u][j];
        }
    }

    auto dist = DistanseFrom(edges, 1);

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

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

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define INF 1e9
#define EXIST 1
int graph[200][200];
int dist[200];
int n;

void bfs() {
  queue<int> next;
  next.push(0);
  dist[0] = 0;
  
  while (!next.empty()) {
    int current = next.front();
    next.pop();
    for (int i = 0; i < n; i++) {
      if (graph[current][i] == EXIST && dist[i] > dist[current]+1) {
        next.push(i);
        dist[i] = dist[current]+1;
      }
    }
  }  
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    dist[i] = INF;
    int u, k, v; cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      graph[u-1][v-1] = EXIST;
    }
  }
  
  bfs();
  
  for (int i = 0; i < n; i++) {
    cout << i+1 << " ";
    cout << (dist[i] == INF ? -1 : dist[i]) << endl;
  }
}

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

#define MAX 100
#define NIL -2

int n, G_list[MAX+1];
int G[MAX+1][MAX+1];
int d[MAX+1][2];

void init(void){
  for(int i=0; i<=MAX; i++){
    d[i][0]=-1;
  }
}

void make_graph(void){
  scanf("%d", &n);

  for(int i=0; i<n; i++){
    int cnt, num, m;

    scanf("%d %d", &num, &cnt);
    G_list[i]=num;

    for(int j=0; j<cnt; j++){
      scanf("%d", &m);
      G[num][m]=1;
    }
  }
}

void check_depth(void){
  queue<int> Q;
  Q.push(G_list[0]);
  d[G_list[0]][0]=0;
  d[G_list[0]][1]=NIL;
  
  while(!Q.empty()){
    int top=Q.front();
    Q.pop();
    
    for(int j=0; j<n; j++){
      if(G[top][G_list[j]] == 1 && d[G_list[j]][1] != NIL){
	d[G_list[j]][0]=d[top][0]+1;
	d[G_list[j]][1]=NIL;
	Q.push(G_list[j]);
      }
    }
  }
}

void print(void){

  for(int i=0; i<n; i++){
    printf("%d %d\n", G_list[i], d[G_list[i]][0]);
  }
}

int main(void){

  init();
  make_graph();
  check_depth();
  print();
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
using vll=vector<ll>;
using vi=vector<int>;
using vb=vector<bool>;
using pii=pair<int,int>;

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}

//pow(llpow,modpow)
template<class T> ll llpow(ll x,T n){ll ans=1;if(x==0)ans=0;while(n){if(n&1)ans*=x;x*=x;n>>=1;}return ans;}
long long modpow(long long a, long long n, long long mod) {long long res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}
//最大公約数
template<class T> inline T gcd(T x,T y){if(y==0)return x; else {return gcd(y,x%y);}}
//最小公倍数
template<class T> inline T lcm(T x,T y){return x/gcd(x,y)*y;}
//逆元
long long modinv(long long a, long long m) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)

long long INF = 1LL<<60;
int main( ){
    int n;
    cin>>n;
    vi conne[n];
    rep(i,0,n){
        int id;cin>>id;
        id--;
        int r; cin>>r;
        rep(j,0,r){
            int in; cin>>in; in--;
            conne[id].push_back(in);
        }
    }
    vi dist(n,-1);
    queue<pii> q;
    q.emplace(0,0);
    while(!q.empty()){
        pii n=q.front(); q.pop();
        int i=n.first; int d=n.second;
        if(dist[i]==-1)dist[i]=d;
        for(int itr:conne[i]){
            if(dist[itr]!=-1)continue;
            q.emplace(itr,d+1);
        }
    }
    rep(i,0,n)cout<<i+1<<" "<<dist[i]<<endl;
    return 0;
}