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++: //
//
//  algoC.cpp - created by us162022
//
#include <iostream>
#include <queue>
using namespace std;
struct Node{
  int u,k,v[99];
  int distance;
};

int main(int argc, char *argv[])
{
  int n;
  Node node[100];
  queue<Node> que;

  cin >> n;

  for(int i=0; i<n; i++){
    cin >> node[i].u >> node[i].k;

    for(int j=0; j<node[i].k; j++)
      cin >> node[i].v[j];

    node[i].distance=-1;
  }

  node[0].distance = 0;
  que.push(node[0]);

  while(!que.empty()){
    Node fro = que.front();

    for(int i=0; i<fro.k; i++){
      int x = fro.v[i]-1;
  
      if(node[x].distance==-1)
	    node[x].distance = fro.distance+1;

      que.push(node[x]);
    }

    que.pop();
  }

  for(int i=0; i<n; i++)
    cout << node[i].u << " " << node[i].distance << endl;

}

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

int A[100][100];//隣接行列
int n;
int D[100];  //訪問状態を保持　距離で

void bfs(int s) {
	queue<int> Q;
	Q.push(s);
	D[s] = 0; 
	while (! Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int d=0; d<n; ++d) {
			if (A[cur][d] == 1 && D[d] == -1) {
				D[d] = D[cur]+1;
				Q.push(d);
			}
		}
	}
}

int main() {
	cin >> n;
	int p,q,r;
	for (int i=0; i<n; ++i) {
		cin >> p >> q;
		D[i] = -1; //到達不可能の時-1
		for (int i=0; i<q; ++i) {
			cin >> r;
			A[p-1][r-1] = 1;
		}
	}
	bfs(0);
	for (int i=0; i<n; ++i) {
		cout << i+1 << " " << D[i] << endl;
	}
    return 0;
}

C++: #include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int main(void)
{
	int i,u,j,n1,c1;
	int n,k[101],v[101][100],c[101];
	int q[100],nn[100],p1,p2;
	scanf("%d",&n);
	for(i=1;i<=n;i++)	{
		scanf("%d %d",&u,&k[i]);
		for(j=0;j<k[i];j++)	{
			scanf("%d",&v[i][j]);
		}
	}
	for(i=1;i<=n;i++)	c[i]=-1;
	c[1]=0;
	p1=0;	p2=0;
	q[p1]=1;	nn[p1]=0;
	p1++;
	c[1]=0;
	while(p1!=p2)	{
		n1=q[p2];	c1=nn[p2];
		p2++;
//		printf("n1=%d c1=%d\n",n1,c1);
		for(i=0;i<k[n1];i++)	{
//			printf("v=%d\n",v[n1][i]);
			if(c[v[n1][i]]==-1)	{
				q[p1]=v[n1][i];
				nn[p1]=c1+1;
//				printf("q=%d nn=%d\n",v[n1][i],c1+1);
				p1++;
				c[v[n1][i]]=c1+1;
			}
		}
	}
	for(i=1;i<=n;i++)	printf("%d %d\n",i,c[i]);
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#define whi 0
#define gra 1
#define bla 2
using namespace std;
int n,d[101],color[101];
bool g[101][101];
queue<int> num;
void dft(){
	int a,i,j;
	for(i=1;i<=n;i++){
	color[i]=whi;
		d[i]=-1;
	}
	num.push(1);
	d[1]=0;
	color[1]=gra;
	while(!num.empty()){
		a=num.front();
		num.pop();
		for(int v=1;v<=n;v++){
			if(g[a][v] && color[v]==whi){
				num.push(v);
				color[v]=gra;
				d[v]=d[a]+1;
			}
			color[a]=bla;
		}
	}	
}
main(){
	int u,k,v,i,j;
	cin>>n;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++)
		g[i][v]=false;
	}
	for(i=1;i<=n;i++){
	cin>>u>>k;
		for(j=1;j<=k;j++){
		cin>>v;
			g[u][v]=true;
		}
	}
	dft();
	for(i=1;i<=n;i++)
	cout<<i<<" "<<d[i]<<endl;
	return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=0; i<n; i++) d[i] = INFTY;
    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] != INFTY) continue;
            cout <<v<<" "<<d[v];
            d[v] = d[u] + 1;
            cout <<"->"<< d[v] << endl;
            q.push(v);
        }
    }
    for(int i=0; i<n; i++){
        cout << i+1 << " " << ((d[i] == INFTY) ? (-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);
    
    return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);
int n, M[N][N];
int d[N];
void bfs(int s) {
    queue<int>q;
    q.push(s);
    for (int i = 0; i < n; i++)d[i] = INFTY;
    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] != INFTY)continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << ((d[i] == INFTY) ? (-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);
    return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n,M[N][N];
int d[N];

void bfs(int s) {
queue<int> q;
q.push(s);
for(int i=0;i<n;i++) d[i] == INFTY;
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] != INFTY)continue;
    d[v] = d[u] +1;
    q.push(v);
  }
}
for(int i= 0;i<n;i++) {
  cout << i+1<<" "<<((d[i] == INFTY) ? (-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);

  return 0;
}