Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

const int INF=99999999;

void get_dinetwork(int N,vector<vector<int>>& array){
    int u,k,x;

    for(int i=0;i<N;i++){
        cin>>u>>k;
        for(int j=0;j<k;j++){
            cin>>x;
            array[u-1][x-1]=1;
        }
    }

}

int bfs(int n,vector<vector<int>>& A,int v,vector<int>& d,vector<int>& flag){
        d[v]=0;
        flag[v]=1;
        queue<int> Q;
        for(int i=0;i<n;i++){if(A[v][i]==1){Q.push(i);}}
        int dmin;
        while(!Q.empty()){
                dmin=INF;
                for(int i=0;i<n;i++){if(A[i][Q.front()]==1 && d[i]<dmin){dmin=d[i];}}
                d[Q.front()]=dmin+1;
                flag[Q.front()]=1;
                for(int i=0;i<n;i++){if(A[Q.front()][i]==1 && flag[i]!=1){Q.push(i);}}
                Q.pop();
        }
}

int main(){
        cin.tie(0);
        ios::sync_with_stdio(false);

        int n;
        cin>>n;
        vector<vector<int>> array(n,vector<int>(n,0));
        get_dinetwork(n,array);

        //
        vector<int> d(n,INF);
        vector<int> flag(n,-1);
        //
        bfs(n,array,0,d,flag);
        //

        for(int i=0;i<n;i++){
                if(d[i]==INF){d[i]=-1;}
                cout<<i+1<<" "<<d[i]<<"\n";
        }

        return 0;

}

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

int N=100;
int INF = 1234567890;
int n,M[100][100];
int d[100];
void bfs(int s){
  queue<int> q;
  q.push(s);
  for(int i=0;i<n;i++) d[i] = INF;
  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]!=INF) continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    if(d[i]==INF) cout<<i+1<<" "<<-1<<endl;
    else cout<<i+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);

}