Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

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

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			if !dl.isVisited(v) {
				stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
			}
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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 <stdio.h>
#include <vector>
#include <queue>

using namespace std;
int ans[100][100] = {};
int now = 0, n;

int main() {
    int u, k, tmp;
    cin >> n;
    vector<int> dist(n, -1);
    queue<int> que;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> tmp;
            ans[u - 1][tmp - 1] = 1;
        }
    }
/*
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; ++j) {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
*/
    dist[0] = 0;
    que.push(0);
    while (!que.empty()) {
        int v = que.front();
        //cout << v;
        que.pop();

        for (int i = 0; i < n; ++i) {
            int nv = ans[v][i];
            if (dist[i] == -1 and nv != 0) {
                dist[i] = dist[v] + 1;
                que.push(i);
            }
        }
        //cout << endl;
    }
    for (int i = 0; i < n; ++i) {
        cout << i + 1 << " " << dist[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];  // 距離で訪問状態（color）を管理する

void bfs(int s) {
  queue<int> q;  // 標準ライブラリの queue を使用
  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<bits/stdc++.h>
using namespace std;
int vis[111],G[111][111],cn,n,d[111];
int  bfs(int i){
  	queue<int>q;
  	q.push(i);
  	vis[i]=1;
	while(!q.empty()){
		int temp=q.front();
		vis[temp]=1;
		q.pop();
		for(int j=1;j<=n;j++){
			if(vis[j]==0&&G[temp][j]!=0){
				d[j]=d[temp]+1;
				vis[j]=1;
				q.push(j);
			}
		}
	}  
}

int main(){
    scanf("%d",&n);
	for(int i=1;i<=n;i++){	
	int v2,v1,k;
		scanf("%d%d",&v1,&k);
		for(int j=0;j<k;j++){
			scanf("%d",&v2);
			G[v1][v2]=1;
		}
}
    bfs(1);
    for(int i=1;i<=n;i++) {
    	if(i==1)
    	printf("%d %d\n",i,d[i]);
    	else if(d[i]==0){
    			printf("%d %d\n",i,-1);
		} 
		else printf("%d %d\n",i,d[i]);
	}
	 
	 return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<n;i++)

using namespace std;


int main()
{
	int n;
	cin>>n;
	
	bool adj[n+10][n+10]={};
	
	for(int i=1;i<n+1;++i){//1index
		
		int u;
		int k;
		cin>>u>>k;
		
		for(int j=1;j<k+1;++j){//1index
			int v;
			cin>>v;
			adj[i][v]=true;
		}
	}
	
	int d[n+10];
	for(int i=1;i<n+1;++i){
		d[i]=-1;
	}
	
	d[1]=0;
	
	queue<int> que;
	que.push(1);
	while(!que.empty()){
		int now=que.front();
		que.pop();
		
		int a=d[now];
		
		for(int j=1;j<n+1;++j){
			
			if((adj[now][j]) and (d[j]==-1)){
				d[j]=a+1;
			que.push(j);
			
			}
			
		}
	}
	
	for(int i=1;i<n+1;++i){
		cout<<i<<" "<<d[i]<<endl;
	}
	
}

C++: #include <iostream>
#include <queue>
using namespace std;
    queue<int> Q;
int u[1000][1000];
int d[1000],color[1000];
// white 0 gray 1 black 2
void BFS(int s,int n){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            color[i]=0;
            d[i]=-1;
        }
    }
    color[s]=1;
    d[s]=0;
    Q.push(s);
    while(Q.size()!=0){
        int tmp=Q.front();
        Q.pop();
        for(int j=1;j<=n;j++){
            if(color[j]==0 && u[tmp][j]==1){
                color[j]=1;
                d[j]=d[tmp]+1;
                Q.push(j);
            }
        }
        color[tmp]=2;
    }
}
int main(){
    int n,l,k,m;
    cin>>n;
    u[100][100]={};
    for(int i=0;i<n;i++){
        cin>>l>>k;
        for(int j=1;j<=k;j++){
            cin>>m;        
            u[l][m]=1;
        }
    }
    BFS(1,n);
    for(int i=1;i<=n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
    return 0;
}