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++: //
//
//  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;
}