Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

using namespace std;

int D[110]={0};
void bfs(int src,int n,int g[110][110]){

queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
	int cur =Q.front();
	Q.pop();
	for(int r=0;r<n;r++){
		if (g[cur][r]==1&&D[r]==0){
			D[r]=D[cur]+1;
			Q.push(r);
		}
	}
}
	
}



int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
	cin>>t;
	g[k][t-1]=1;
}
}
bfs(0,n,g);
for(int a=0;a<n;a++){
	cout<<a+1<<" "<<D[a]<<endl;
}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

int head, tail;
int Q[100000]; //queue
int n;
int d[110]; //distance
bool a[110][110] = {}; //adjacency matrix

//queue
void enqueue(int x){
  
  Q[tail] = x;

  if(tail + 1 == 100000) tail = 0;
  else tail++;
}

int dequeue(){
  
  int x = Q[head];

  if(head + 1 == 100000) head = 0;
  else head++;

  return x;
}

void bfs(int u){
  
  enqueue(u);
  d[u] = 0;
  
  while(head != tail){
    int p = Q[head];
    dequeue();
    
    for(int i = 2;i <= n;i++){
      //if i is adjoining p and have not been visited, calculate the distance
      if(a[p][i] && d[i] == 0){
        enqueue(i);
        d[i] = d[p] + 1;
      }
    }
  }
}

int main(){
  
  int u, k, v;

  //input
  cin >> n;
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> v;
      a[u][v] = true;
    }
  }
  
  bfs(1);

  //output
  for(int i = 1;i <= n;i++){
    if(d[i] || i == 1) cout << i << " " << d[i] << endl;
    else cout << i << " " << -1 << endl;
  }
  
  return 0;
}

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

const int mod = 1e9+7;
const int INF = 2000000;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> G(n+1);
    for(int i=1;i<=n;++i) {
        int u,k;
        cin >> u >> k;
        for(int j=0;j<k;++j) {
            int v;
            cin >> v;
            G[u].push_back(v);
        }
    }
    queue<int> q;
    vector<int> cnt(n+1,-1);
    cnt[1] = 0;
    q.push(1);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int i:G[u]) {
            if(cnt[i] != -1) continue;
            cnt[i] = cnt[u] + 1;
            q.push(i);
        }
    }
    for(int i=1;i<=n;++i) cout << i << " " << cnt[i] << endl;
}