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

using Graph = std::vector<std::vector<uint32_t>>;

int main() {
  uint32_t n = 0;
  std::cin >> n;
  Graph G(n + 1);
  for (size_t i = 1; i < n + 1; i++) {
    uint32_t k = 0;
    std::cin >> i >> k;
    if (k > 0) G[i] = std::vector<uint32_t>(k);
    for (auto&& g : G[i]) std::cin >> g;
  }

  std::vector<int32_t> dist(n + 1, -1);
  dist[1] = 0;
  std::queue<uint32_t> Q({1});
  while (!Q.empty()) {
    uint32_t node = Q.front();
    Q.pop();
    for (auto&& to : G[node]) {
      if (dist[to] != -1) continue;
      Q.push(to);
      dist[to] = dist[node] + 1;
    }
  }

  for (size_t i = 1; i < dist.size(); i++) {
    std::cout << i << " " << dist[i] << std::endl;
  }

  return 0;
}

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

int n;
int k;
int m;

int main() {
  cin >> n;
  int V[n];

  // int型の2次元配列(3×4要素の)の宣言
  vector<vector<int>> A(n, vector<int>(n));

  for (int i=0; i<n; i++) {
    cin >> V[i];
    cin >> k;
    if (k==0) {
      continue;
    } else {
      for (int j =0; j<k; j++) {
        cin >> m;
        A[i][m-1] = 1;
      }
    }
  }

  int D[n];
  for (int i=0; i<n; i++) D[i] = -1;
  D[0] = 0;

  queue<int> Q;
  Q.push(0);
  while(! Q.empty()) {
    int cur = Q.front();
    Q.pop();

    for (int i=0; i<n; i++) {
      if (A[cur][i] == 1 && D[i] == -1) {
        D[i] = D[cur] + 1;
        Q.push(i);
      }
    }
  }

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

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;
 
     
     
}