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>
#include<vector>
using namespace std;
int main(){
  queue<int> q;
  int n,a,k,u,c[101];
  vector<int>  d[101];
  cin>>n;for(int i=0;i<n;i++){
    cin>>u>>k;
    d[u].resize(k);
    for(int j=0;j<k;j++) cin>>d[u][j];
  }for(int i=0;i<=n;i++) c[i]=-1;
  q.push(1),c[1]=0;
  while(!q.empty()){
   a=q.front(),q.pop();
    for(int i=0;i<d[a].size();i++)
      if( d[a][i]>1 && c[d[a][i]]==-1) q.push(d[a][i]),c[d[a][i]]=c[a]+1;
  } for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

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

struct node {
	std::vector<int> child;
	int distance;
};

int main() {
	int node_num;

	std::cin >> node_num;

	std::vector<node> n(node_num);

	for (int i = 0; i < node_num; i++) {
		int num, to, node_val;
		std::cin >> node_val >> num;
		for (int j = 0; j < num; j++) {
			std::cin >> to;
			n[node_val - 1].child.push_back(to);
		}
		n[i].distance = -1;
	}

	//???????????????????????¢0
	n[0].distance = 0;
	std::queue<int> q;
	int dist = 1;
	q.push(1);
	while(q.size() != 0){
		int num = q.front();
		q.pop();
		for (std::vector<int>::iterator it = n[num - 1].child.begin(); it != n[num - 1].child.end(); it++) {
			if (n[*it - 1].distance > dist || n[*it - 1].distance == -1) {
				n[*it - 1].distance = dist;
				q.push(*it);
			}
		}
		dist++;
	}

	for (int i = 0; i < node_num; i++) {
		std::cout << i + 1 << " " << n[i].distance << std::endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const int INF = 1e9;
const int MOD = 1e9+7;
using Graph = vector<vector<ll>>;
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;}

// 0 false, 1 true 
// string to int : -48
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

int main() {
    std::cout << std::fixed << std::setprecision(15);
    ll n;
    cin >> n;
    vector<vector<ll>> g(n);
    rep(i, n){
        ll num;
        cin >> num;
        num--;
        ll edgenum;
        cin >> edgenum;
        rep(j,edgenum){
            ll next;
            cin >> next;
            next--;
            g[num].push_back(next);
        }
    }

    queue<ll> que;
    que.push(0);
    vector<ll> ans(n, -1);
    ans[0] = 0;
    while(!que.empty()){
        ll now = que.front();
        ll dist = ans[now];
        que.pop();
        for(ll i = 0; i < g[now].size(); i++){
            if(ans[g[now][i]] != -1){
                continue;
            }

            ans[g[now][i]] = ans[now] + 1;
            que.push(g[now][i]);

        }
    }

    rep(i,g.size()){
        cout << i+1 << " " << ans[i] << endl;
    }



}

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

#define endl '\n'
#define ll long long
#define ui unsigned int
#define ull unsigned long long

int main(void)
{
    int n;
    while (cin >> n)
    {
        int adj[105][105]={{0}};
        int tmpn = n;
        while (tmpn--)
        {
            int u, k;
            cin >> u >> k;
            for (int i = 0 ; i < k ; i++)
            {
                int tmp;
                cin >> tmp;
                adj[u-1][tmp-1] = 1;
            }
        }

        int dis[105]={0};
        queue< pair<int, int> > g;
        g.push( make_pair(1,0) );
        while (!g.empty())
        {
            for (int i = 0 ; i < n ; i++)
            {
                if (adj[g.front().first-1][i] == 1 && dis[i] == 0)
                {
                    g.push( make_pair(i+1, g.front().second+1) );
                    dis[i] = g.front().second+1;
                }
            }
            g.pop();
        }

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

    return 0;
}

C++: //push(),pop():C++ではstackと統一されいる
//front():先頭の要素が何かを返す
//back():末尾の要素が何であるかを返す
//empty():キューが空であるかどうかを調べる
//size():要素数を調べる
#include <iostream>
#include <queue>
using namespace std;
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
static const int INFTY = (1<<21);

int G[MAX][MAX],color[MAX],d[MAX],num;

void bfs(int s){
  int u;
  queue<int> Q; //Qを使うことでエンキュー、デキューの操作が容易になる
  for(int i=1; i<=num; i++) {
    d[i] = INFTY;
  }
  Q.push(s);
  d[s] = 0;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();
    for(int v=1; v<=num; v++){
      if(G[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}


int main(){
  int i,j,u,k,v;

  scanf("%d",&num);
  for(i=1; i<=num; i++){
    color[i] = WHITE;
    for(j=1; j<=num; j++){
      G[i][j] = 0;
    }
  }
  for(i=1; i<=num; i++){
    scanf("%d%d",&u,&k);
    for(j=1; j<=k; j++){
      scanf("%d",&v);
      G[u][v] = 1;
    }
  }

  bfs(1);

  for(i=1; i<=num; i++) printf("%d %d\n",i,d[i]);

  return 0;
}

C++: #include <cstdio>

void search(int (*A)[101], int *dis, int n){
    int hoge;
    int time=0;
    for(int k=1;k<n;k++){
        hoge = 1;
        for(int i=1;i<=n;i++){
            if(dis[i]==time){
                for(int j=1;j<=n;j++){
                    if(A[i][j]&&dis[j]<0){
                        dis[j]=time+1;
                        hoge=0;
                    }
                }
            }
        }
        time++;
        if(hoge)return;
    }
}

int main(){
    int i, j, n, u, k, v;
    scanf("%d",&n);
    int dis[n+1];
    dis[1]=0;
    for(i=2;i<=n;i++){
        dis[i]=-1;
    }
    int A[101][101];
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            A[i][j]=0;
        }
    }
    for(i=1;i<=n;i++){
        scanf("%d %d",&u,&k);
        for(j=0;j<k;j++){
            scanf("%d",&v);
            A[u][v]=1;
        }
    }
    search(A,dis,n);
    for(i=1;i<=n;i++)printf("%d %d\n",i,dis[i]);
    return 0;
}