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<bits/stdc++.h>

struct node{
    int d, flag;
    node(){
        d = 0;
        flag = 0;
    }
};

int main(void){
    int n;
    int ind, num, a;
    int time, ndi;

    std::cin >> n;
    std::vector<node> nd(n);
    std::vector<std::vector<int>> b(n, std::vector<int>(n));
    std::queue<int> st;

    for(int i = 0; i < n; i++){
        std::cin >> ind >> num;
        ind--;
        for(int i = 0; i < num; i++){
            std::cin >> a;
            a--;
            b[ind][a] = 1;
        }
    }

    time = 0;
    int x = 0;
    //for(int x = 0; x < n; x++){
        if(nd[x].flag == 0){
            ndi = x;
            time++;
            nd[x].flag = 1;
        
            st.push(x);
        }
    
        

        while(st.empty() != 1){
            //std::cout << ndi+1 << " " << time << std::endl;
            //time++;
            int tmp = -1;
            int tmp2;
            for(int i = 0; i < n; i++){
                if(b[ndi][i] == 1 && nd[i].flag == 0){
                    tmp = i;
                    tmp2 = x;
                    break;
                }
            }
            if(tmp == -1){
                //std::cout << ndi+1 << " " << time << std::endl;

                nd[ndi].flag = 1;
                st.pop();
                if(st.empty() != 1){
                    ndi = st.front();
                }

                continue;
            }

            nd[tmp].flag = 1;
            nd[tmp].d = nd[ndi].d + 1;
            st.push(tmp);
            
        }
    //}
    for(int i = 0; i < n; i++){
        if(nd[i].flag == 0 && i != 0){
            nd[i].d = -1;
        }
    }
    for(int i = 0; i < n; i++){
        std::cout << i+1 << " " << nd[i].d << std::endl;
    }

    return 0;
}

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

using namespace std;

int color[100];
int d[100];
int f[100];
int G[100][100];
int nt[100];
int n;

int WHITE = 1;
int GRAY = 2;
int BLACK = 3;

int _time = 0;

int next(int u) {
  for (int v = nt[u]; v < n; v++) {
    nt[u] = v + 1;
    if (G[u][v]) return v;
  }

  return -1;
}

void dfs(int r) {
  for (int i = 0; i < n; i++) nt[i] = 0;

  queue<int> S;

  S.push(r);
  color[r] = GRAY;

  while(!S.empty()){
    int u = S.front();

    while(1) {
      int v = next(u);

      if(v != -1) {
        if(color[v] == WHITE) {
          color[v] = GRAY;

          S.push(v);

          d[v] = d[u] + 1;
        }
      } else {
        break;
      }
    }

    S.pop();
  }
}

void dfs_init() {
  for(int i = 0; i < n; i++) {
    color[i] = WHITE;
  }

  dfs(0);

  for(int i = 0; i < n; i++) {
    if(color[i] == WHITE) {
      d[i] = -1;
    }
  }
}

int main() {
  int m, k, l;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    cin >> m;

    for (int j = 0; j < m; j++) {
      int l;
      cin >> l;

      G[k-1][l-1] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    dfs_init();
  }

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

C++: #include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;

vector<int>E[100];
int d[100];
int main(){
	int n;cin>>n;
	rep(i,n){
		int u,k;cin>>u>>k;u--;
		rep(i,k){
			int v;cin>>v;v--;
			E[u].push_back(v);
		}
	}
	memset(d,0x3f,sizeof(d));
	queue<P>que;
	d[0]=0;que.push(P(0,0));
	while(!que.empty()){
		P p=que.front();que.pop();
		for(int u:E[p.second]){
			if(d[u]>p.first+1){
				d[u]=p.first+1;
				que.push(P(d[u],u));
			}
		}
	}
	rep(i,n){
		if(d[i]==INF)cout<<i+1<<" -1"<<endl;
		else cout<<i+1<<' '<<d[i]<<endl;
	}
}

C++: // 幅優先探索
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

#include<queue>

using namespace std;

#define MAX_N 100
// 値の定義
const int INV = -1;
const int EXIST = 1;
const int NO_EXIST = 0;

const int YES = 0;
const int NO = -1;

// グラフ情報を保持する配列
int ary_graph[MAX_N + 1][MAX_N + 1];

// 頂点１からの距離を保持する配列
int vertex_distance[MAX_N + 1];
// 頂点１からの訪問状態を保持する配列
int ary_visit[MAX_N + 1];

int n = 0;
int nt[MAX_N + 1];

void widthFirstSearch(int start_vertex);

int main(void)
{
    int u, num, v_num, start_point;

    for(int i = 0; i < MAX_N + 1; i++)
    {
    	vertex_distance[i] = 0;
        ary_visit[i] = NO;

        for(int j = 0; j < MAX_N + 1; j++)
        {
            // 配列の初期化
            ary_graph[i][j] = NO_EXIST;
        }
    }

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> u >> num;

        // 0オリジンにする
        u--;

        for(int j = 0; j < num; j++)
        {
            // 隣接点を記憶する
            cin >> v_num;

            // 0オリジンにする
            v_num--;
            ary_graph[u][v_num] = EXIST;
        }
    }

    for(int i = 0; i < n; i++)
    {
        if( ary_visit[i] == NO )
        {
            start_point = i;

            // 幅優先探索を実施
            widthFirstSearch(i);
        }
    }

    // 結果の表示
    for(int i = 0; i < n; i++)
    {
        if( i != start_point && vertex_distance[i] == 0 )
        {
            // 開始点から到達不可能な点だった
            vertex_distance[i] = INV;
        }

        printf("%d %d\n", i + 1, vertex_distance[i]);
    }

    return 0;
}

// u に隣接する v を番号順に取得
int next(int u)
{
    int ret = INV;

    for(int v = nt[u]; v < n; v++)
    {
        nt[u] = v + 1;

        if( ary_graph[u][v] == EXIST )
        {
            // 隣接点が存在する
            ret = v;
            break;
        }
    }

    return ret;
}

// 幅優先探索を実施する関数
// start_vertex : 起点となる頂点のNo.
// vertex_num : グラフの頂点数
void widthFirstSearch(int start_vertex)
{

    queue<int> Q;

    Q.push(start_vertex);
    vertex_distance[start_vertex] = 0;
    ary_visit[start_vertex] = YES;

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

        for(int i = 0; i < n; i++)
        {
            // 隣接点を全て検索する
            int v = next(u);

            if( v == INV )
            {
                // 隣接点が無くなったらキューから削除して, 処理終了
                Q.pop();
                break;
            }

            if( ary_visit[v] == NO
                && ary_graph[u][v] == EXIST )
            {
                // 訪問済み
                ary_visit[v] = YES;

                Q.push(v);
                vertex_distance[v] = vertex_distance[u] + 1;
            }
        }
    }

    return;
}