Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

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

using namespace std;

int main() {
	int n, u, k, v;
	cin >> n;
	vector<int> ans(n,1000000000);
	vector<vector<int>> adj(n);
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		while (k--)cin >> v, adj[u-1].push_back(v-1);
	}
	queue<pair<int, int>> q;//<idx,path>
	q.push(pair<int, int>(0, 1));
	while (!q.empty()) {
		auto tmp = q.front(); q.pop();
		ans[tmp.first] = min(ans[tmp.first], tmp.second);
		if (ans[tmp.first] != tmp.second)continue;
		for (int i = 0; i < adj[tmp.first].size(); i++) q.push(pair<int, int>(adj[tmp.first][i], tmp.second+1));
	}
	for (int i = 0; i < n; i++)	cout << i + 1 << " " << (ans[i] == 1000000000 ? -1 : ans[i]-1) << endl;
	
}

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

using namespace std;

int D[100100];//始点からの距離を入れる
int g[1001][1001]={{0}};//要素を0で初期化
void bfs(int src,int n){//srcは開始位置この場合だと頂点1だから0
  cerr << "bfs_root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()  ){
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting_" << cur << " " <<D[cur] <<endl;
    for(int dst= 0; dst < n; dst++){
      if(g[cur][dst]==1 && D[dst]==0){
        D[dst]=D[cur]+1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n){
  for (int i = 0; i < n; ++i)
  {
    cout << i+1 <<" "<<D[i]<<endl;
  }

}

int main(){
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n,a,b,c;
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> a >> b ;
    for (int i = 0; i < b; ++i)
    {
      cin >> c;
      g[a-1][c-1]=1;/* code */
    }
  }
  bfs(0,n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }


}

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

using namespace std;

typedef pair<int, int> E;

int INF = -1;

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

  int cnt = 0;
  int* visit = new int[n+1];
  for (int i = 1; i <= n; i++) {
    visit[i] = INF;
  }

  queue<E> q;
  q.push(E(1, cnt));
  while (q.size()) {
    E e = q.front();
    q.pop();
    if (visit[e.first] != INF)
      continue;
    visit[e.first] = e.second;

    int len = G[e.first][0];
    for (int i = 1; i <= len; i++) {
      q.push(E(G[e.first][i], e.second + 1));
    }
  }

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

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

using namespace std;

const int no_edge = -1;
const int edge = 1;
const int first_visit_label = 1;
const int no_distance = -1;

int main() {
  int vertex_num;
  cin >> vertex_num;

  vector<vector<int>> matrix_graph(
    vertex_num,
    vector<int>(vertex_num, no_edge)
  );

  for(int i = 0; i < vertex_num; i++) {
    int vertex_label;
    int adjacency_num;
    cin >> vertex_label >> adjacency_num;
    for(int j = 0; j < adjacency_num; j++) {
      int adjacency_label;
      cin >> adjacency_label;
      matrix_graph[vertex_label - 1][adjacency_label - 1] = edge;
    }
  }

  queue<int> will_visit_labels;
  vector<int> distances(vertex_num, no_distance);
  distances[first_visit_label - 1] = 0;
  will_visit_labels.push(first_visit_label);

  while(!will_visit_labels.empty()) {
    int current_label = will_visit_labels.front();
    will_visit_labels.pop();

    for(int i = 0; i < vertex_num; i++) {
      int will_visit_label = i + 1;
      if(matrix_graph[current_label - 1][will_visit_label - 1] == no_edge) {
        continue;
      }
      if(distances[will_visit_label - 1] != no_distance) {
        continue;
      }
      distances[will_visit_label - 1] = distances[current_label - 1] + 1;
      will_visit_labels.push(will_visit_label);
    }
  }

  for(int i = 0; i < vertex_num; i++) {
    cout << i + 1 << ' ' << distances[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];
  rep(i,n){
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
       d[i][j]=100000;
      }
    }
  }
  rep(i,n){
    int u,k;
    cin >> u >> k;
    q1.push(u);
    if(k>0){
     int v[k];
     rep(j,k){
       cin >> v[j];
       q1.push(v[j]);
     }
     int f = q1.front()-1;
     q1.pop();
     rep(i,k){
       int e=q1.front()-1;
       d[f][e]=1;
       q1.pop();
     }
    }else{
     q1.pop();
    }
  }

  rep(i,n){
    for(j=2;j<n;j++){
    d[0][i]=min(d[0][i],d[0][j]+d[j][i]);
    }
  }
 
 rep(i,n){
   cout << i+1 << ' ' <<d[0][i] << endl;
 }
 
  return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = -1;

int n, M[N][N];
int d[N];

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

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