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 <queue>
#include <vector>


class Graph {
  public:
    Graph(int n) : n(n) {
        adj_arr = std::vector<std::vector<int>>(n);
        depth = std::vector<int>(n, -1);
    }

    void breadth_search(int i, int d) {
        std::queue<int> queue;
        depth[i] = d;
        
        for (auto && v : adj_arr[i]) {
            if (depth[v - 1] == -1 || depth[v - 1] > d) {
                queue.push(v - 1);
            }
        }

        while (!queue.empty()) {
            int v = queue.front();
            queue.pop();
            breadth_search(v, d + 1);
        }
    }

    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i + 1 << " " << depth[i] << std::endl;
        }
    }

  public:
    std::vector<std::vector<int>> adj_arr;
    std::vector<int> depth;
    
  private:
    int n;
};


int main () {
    int n;
    std::cin >> n;

    Graph graph(n);

    for (int i = 0; i < n; i++) {
        int u, k;
        std::cin >> u >> k;
        std::vector<int> arr(k);
        
        for (int j = 0; j < k; j++) {
            std::cin >> arr[j];
        }
        graph.adj_arr[u - 1] = arr;
    }

    graph.breadth_search(0, 0);
    graph.print();
}

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

vector<vector<int>> v;
vector<int> d, t;

void bfs(){
  int time = 0;
  queue<int> q;
  q.push(0);

  while(!q.empty()){
    int node = q.front();
    q.pop();

    for(int i=0; i<v.size(); ++i){
      if(v[node][i] == 1 && t[i] == -1){
         q.push(i);
         t[i] = t[node] + 1;
       }
    }
  }
}

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

  v.resize(n);
  d.resize(n);
  t.resize(n);
  for(int i=1; i<n; ++i) t[i] = -1;

  for(int i=0; i<n; ++i) v[i].resize(n);

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

  bfs();

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

C++: #include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <queue>

using namespace std;

int n;
int arr[100][100];
int time_saver[100][2];
int now_time;
queue< pair < int, int > > q;

void search(int fi, int fnow_time){
    time_saver[fi][0] = fnow_time;
    for(int i=0; i<100; i++){
        if(arr[fi][i] && !time_saver[arr[fi][i]-1][0]){
            int a = fnow_time+1;
            q.push(make_pair(arr[fi][i]-1, a));
        }
    }
}

int main(int argc, const char * argv[]) {
    
    /////////
    //write//
    /////////
    
    cin >> n;
    now_time = 0;
    for(int i=0; i<100; i++){
        for(int j=0; j<100; j++){
            arr[i][j] = 0;
        }
        for(int j=0; j<2; j++){
            time_saver[i][j] = 0;
        }
    }
    
    for(int i=0; i<n; i++){
        int node, degree; cin >> node >> degree;
        for(int j=0; j<degree; j++){
            int tmp; cin >> tmp;
            arr[node-1][tmp-1] = tmp;
        }
    }
    
    for(int i=0; i<1; i++){
        if(time_saver[i][0])continue;
        search(i, now_time);
        while(!q.empty()){
            int tmp = q.front().first;
            int tmp2 = q.front().second;
            now_time = max(now_time, q.front().second+1);
            q.pop();
            if(time_saver[tmp][0] || tmp == 0)continue;
            search(tmp, tmp2);
        }
    }
    for(int i=1; i<n; i++){
        if(!time_saver[i][0])time_saver[i][0] = -1;
    }
    
    for(int i=0; i<n; i++){
        for(int j=0; j<1; j++){
            if(j)cout << " ";
            else cout << i+1 << " ";
            cout << time_saver[i][j];
        }
        cout << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
 
int n;
vector <int> G[101];
queue <int> Q;
int  d[100];
int  dis[101];
 
 
int visit(int x){
  int t,i;
   
  Q.push(x);
  dis[x] = 0;
  d[x] = 1;
 
  while(Q.empty() == false) {
     
    // cout << "start Q" <<endl;
 
    t = Q.front();
    Q.pop();
 
    for(i=0; i<G[t].size();i++) {
      if(d[G[t][i]] == 0 ) {
    d[G[t][i]] = 1;
    dis[G[t][i]] = dis[t] + 1;
 
    Q.push(G[t][i]);
      }
 
    }
  }
}
     
int main() {
   
  int i,j,k,l,u,a,t;
   
   
  scanf("%d",&n);
 
   
   
  for(i=0;i<n;i++){
    d[i] = 0;
    dis[i] = -1;
    scanf("%d %d ",&k,&u);
     
    for(j=0;j<u;j++) {
      scanf("%d",&a);
      G[k-1].push_back(a-1);
      // printf("G[%d][%d] = 1\n",i,k-1);
    }
  }
   
   
  visit(0);
 
   
 
  for(i=0;i<n;i++) cout << i+1<<" " << dis[i] <<endl;
 
 
 
  return 0;
 
     
     
}

C++: #include <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[102][102];
int A[103],f[103],w,h,n,sx,sy,gx,gy,e,g[103];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<100;i++){		
		A[i]=0;				
		f[i]=-1;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
		//	printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	f[1]=0;
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			f[i]=g[d]+1;
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	return 0;
}