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;
#define MAX 100

const int INF = (1<<21);
int mat[MAX][MAX] = {};
int n, d[MAX] = {};
bool c[MAX];

void bfs(int u){
    queue<int> p; 
    int now = 0;
    d[u] = now++;
    c[u] = true;

    p.push(u);
    while(!p.empty()){
        int x = p.front(); p.pop();
        for(int i = 0; i < n; i++){
            if(mat[x][i] && !c[i]){
                p.push(i);
                c[i] = true;
                d[i] = d[x]+1;
            }
        }
    }
}

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i++){
        int x, k, y;
        cin >> x >> k;
        while(k-- > 0){
            cin >> y;
            mat[x-1][y-1] = 1;
        }
    }

    for(int i = 0; i < n; i++) d[i] = INF;
    
    bfs(0);

    for(int i = 0; i < n; i++){
        cout << (i+1) << " " << (d[i] == INF ? -1 : 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[u]!=0){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,v,k;
    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 <cstdio>
#define MAX 101
using namespace std;

int n;
int a[MAX][MAX]={};
int d[MAX];

void track(int v,int depth){
  if(d[v]>depth||d[v]==-1){
    d[v]=depth;
  }else if(d[v]<depth){
    return;
  }
  for(int i=1;i<=n;i++){
    if(a[v][i]==1){
      track(i,depth+1);
    }
  }
}

int main(){
  cin>>n;
  for(int i=1;i<=n;i++){
    int u,k; cin>>u>>k;
    for(int j=0;j<k;j++){
      int v; cin>>v;
      a[i][v]=1;
    }
  }

  for(int i=1;i<=n;i++){
    d[i]=-1;
  }

  track(1,0);

  for(int i=1;i<=n;i++){
    cout<<i<<" "<<d[i]<<endl;
  }
}

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

int n, d[MAX];
vector<vector<int> > vec;
vector<int> ve;
queue<int> Q;

void bfs(int s) {
	for (int i = 1; i <= n; i++) {
		d[i] = -1;
	}
	d[s] = 0;
	Q.push(s);
	int now, cnt;
	while (!Q.empty()) {
		now = Q.front();
		Q.pop();
		for (cnt = 1; cnt <= vec[now][0]; cnt++) {
			if (d[vec[now][cnt]] == -1) {
				Q.push(vec[now][cnt]);
				d[vec[now][cnt]] = d[now] + 1;
			}
		}
	}
}

int main(void) {
	vec.push_back(ve);
	int u, k, v, i;
	cin >> n;
	for (i = 0; i < n; i++) {
		ve.clear();
		cin >> u >> k;
		ve.push_back(k);
		while (k--) {
			cin >> v;
			ve.push_back(v);
		}
		vec.push_back(ve);
	}

	bfs(1);

	for (i = 1; i <= n; i++) {
		cout << i << " " << d[i] << endl;
	}
}

C++: using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define divup(a,b) ( ( (a) + ( (b) - 1u) ) / (b) ) // a/b の余り切り上げ
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
const ll mod = 1000000007;
const ll mod9 = 1000000009;

ll d[109];
ll u[109];
ll k[109];
ll v[109][109];
struct Point{
    ll s,t;
};
queue<Point> S;

ll  bfs(){
    while(!S.empty()){
        ll n = S.front().s;
        ll t = S.front().t;
        S.pop();
        if(d[n]==0){
            d[n]=t;
        // cout<<" d "<<n<<" "<<t<<endl;
        }
        rep(i,k[n]){
            if(d[v[n][i]-1]==0) S.push({v[n][i]-1,t+1});
        }
    }
    return 0;
}

signed main(){
    ll n;
    cin>>n;
    rep(i,n){
        ll a;
        cin>>u[i]>>k[i];
        rep(j,k[i]){
            cin>>v[i][j];
        }
    }
    S.push({0,1});
    bfs();
    rep(i,n){
        d[i]--;
        printf("%lld %lld\n",i+1,d[i]);
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct id{
    int c[101];
    int p=0;
    int d=-1;
    int u=0;
};
int n,t=0;
id a[101];
queue<int> q;
void bps(int e){
    q.push(e);
    while(!q.empty()){
        int f=q.front();
        for(int i=1;i<=n;i++){
            if(a[f].c[i]==1&&a[i].u==0){
                a[i].d=a[f].d+1;
                q.push(i);
                a[i].u++;
            }
        }
        q.pop();
        a[f].u++;
    }
    
}

int main(){
    int x,y,z;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        for(int j=1;j<=y;j++){
            cin>>z;
            a[x].c[z]=1;
            a[z].p=x;
        }
    }
    a[1].d+=1;
    bps(1);
    for(int i=1;i<=n;i++){
        cout<<i<<" "<<a[i].d<<endl;
    }
    return 0;
}