Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include<iostream>
#include<queue>
using namespace std;
const int MAX = 100;
int n, G[MAX + 1][MAX + 1];
bool book[MAX + 1] = { false };
int path[MAX + 1];
void breathFSearch(int orign)
{
	queue<int> Q;
	Q.push(orign);
	book[orign] = true;
	while (!Q.empty())
	{
		int u = Q.front();
		for (int v = 1; v <= n; v++)
		{
			if (G[u][v] && !book[v])
			{
				Q.push(v);
				book[v] = true;
				if(path[u]!=-1)
					path[v] = path[u]+1;
			}
		}
		Q.pop();
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int u, d;
		cin >> u >> d;
		for (int j = 1; j <= d; j++)
		{
			int v;
			cin >> v;
			G[u][v] = 1;
		}
	}
	for (int i = 1; i <= n; i++)
		path[i] = -1;
	path[1] = 0;
	for(int i=1;i<=n;i++)
		if(!book[i])
			breathFSearch(i);
	for (int i = 1; i <= n; i++)
		cout << i << " " << path[i] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

vector<vector<int> > G;
// bfs 幅優先探索
int bfs(int s)
{
    vector<int> cost(G.size(), -1);

    cost[s] = 0;
    queue<int> pos_queue;
    pos_queue.push(s);

    while (pos_queue.size())
    {
        int pos = pos_queue.front();
        pos_queue.pop();
        rep(i, G[pos].size())
        {
            int next = G[pos][i];

            if (cost[next] == -1)
            {
                cost[next] = cost[pos] + 1;
                pos_queue.push(next);
            }
        }
    }
    rep(i, G.size())
    {
      cout << i + 1 << " " << cost[i] << endl;
    }
}

int main()
{
  int N;
  cin >> N;
  G.resize(N);
  rep(i, N)
  {
    int u, k;
    cin >> u >> k;
    u--;
    rep(j, k)
    {
      int v;
      cin >> v;
      G[u].push_back(v - 1);
    }
  }
  bfs(0);
  return 0;
}

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

int main(){
  int n, u, k, tmp, lp;
  vector<int> d;
  vector< vector<int> > v;
  vector< queue<int> > q;
  
  cin >> n;
  v.resize(n+1);
  d.resize(n+1, 1000000007);
  q.resize(1);
  d[1]=0;
  q[0].push(1);
  
  for (int r = 0; r < n; r++) {
	cin>> u >> k;
    for(int i=0;i<k;i++){
      cin>> tmp;
      v[u].push_back(tmp);
    }
  }
  
  for(int r=0;r<n;r++){
    //cout<< "r=" << r <<endl;
    q.resize(q.size()+1);
    lp=q[r].size();
    for(int i=0;i<lp;i++){
      tmp=q[r].front();
      //cout<< "tmp=" << tmp <<endl;
      q[r].pop();
      for(int j=0;j<v[tmp].size();j++){
        //cout<< "push " << v[tmp][j] << "  val " << min(v[tmp][j], d[tmp]+1) <<endl;
        if(d[v[tmp][j]]>d[tmp]+1){
          q[r+1].push(v[tmp][j]);
      		d[v[tmp][j]]=min(d[v[tmp][j]], d[tmp]+1);
        }
      }
    }
  }
  
  for(int i=1;i<=n;i++){
    cout<< i << " ";
    if(d[i]==1000000007) cout<< "-1" <<endl;
    else cout<< d[i] <<endl;
  }
  
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define REAP(i,a,n) for(int i=(a);i<(n);i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(),v.end()
typedef long long ll;

int table[101][101];

void init(){
    REP(i,101){
        REP(j,101){
            table[i][j]=1e9;
            if(i==j)table[i][j]=0;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    init();
    int u,k,v;
    REP(i,n){
        cin >> u >> k;
        REP(j,k){
            cin >> v;
            table[u-1][v-1]=1;
        }
    }

    REP(k,n){
        REP(i,n){
            REP(j,n){
                table[i][j]=min(table[i][j],table[i][k]+table[k][j]);
            }
        }
    }
    REP(i,n){
        cout << i+1 << " " << ((table[0][i]<n)?table[0][i]:-1);
        cout << endl;
    }
}

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

int main() {
    vector<int> G[101];
    int N; cin >> N;
    for(int i = 0; i < N; i++) {
        int u, k; cin >> u >> k;
        while(k > 0) {
          int v; cin >> v; 
          G[u].push_back(v);
          k--;
        }
    }

    vector<int> dist(N+1, -1);
    queue<int> Q;
    Q.push(1);
    dist[1] = 0;

    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(int i = 0; i < (int)G[u].size(); i++) {
            int v = G[u][i];
            if(dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            Q.push(v);
        }
    }
    for(int i = 1; i < N+1; i++) cout << i << ' ' << dist[i] << endl;
}

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

using namespace std;

#define MAX 100
#define INF 1e9

int n;
int G[MAX][MAX];
int dist[MAX];

void bfs(){
  bool used[MAX];
  fill(dist,dist+n,INF);
  fill(used,used+n,false);
  dist[0] = 0;

  queue<int> Q;
  Q.push(0);

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

    for(int i = 0 ; i < n ; i++){
      if(!used[i] && G[v][i]){
	used[i] = true;
	dist[i] = dist[v] + 1;
	Q.push(i);
      }
    }
  }
}

int main(){
  cin >> n;

  for(int i = 0 ; i < n ; i++){
    int u,k,v;
    cin >> u >> k;
    for(int j = 0 ; j < k ; j++){
      cin >> v;
      G[u-1][v-1] = 1;
    }
  }

  bfs();

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

  return 0;
}