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) {
			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++: //
// Created by tattsun on 2017/02/14.
//

#include <iostream>
#include <vector>

#define VERTEX_NUM_MAX 101

using namespace std;
typedef vector<int> Vertex;

int n;
Vertex vs[VERTEX_NUM_MAX];
int ds[VERTEX_NUM_MAX];

void _bfs(vector<int>& targets, int distance) {
    vector<int> next_targets;

    for(auto it = targets.begin(); it != targets.end(); it++) {
        if(ds[*it] == -1) {
            ds[*it] = distance;

            auto& t = vs[*it];
            for(auto itt = t.begin(); itt != t.end(); itt++)
                next_targets.push_back(*itt);
        }
    }

    if(next_targets.empty())
        return;

    _bfs(next_targets, distance+1);
}

void bfs() {
    vector<int> targets;
    targets.push_back(1);

    _bfs(targets, 0);
}

int main() {
    for(int i=0; i<VERTEX_NUM_MAX; i++) {
        ds[i] = -1;
    }

    cin >> n;

    int u, k, p;
    for(int i=1; i<=n; i++) {
        cin >> u >> k;
        for(int j=0; j<k; j++) {
            cin >> p;
            vs[i].push_back(p);
        }
    }

    bfs();

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

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

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define all(x) (x).begin(), (x).end()
using i32 = int32_t;
using i64 = int64_t;
using f32 = float;
using f64 = double;
using P   = pair<int, int>;

template <class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template <class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void dump_vec(const vector<T>& v) {
    auto len = v.size();
    rep(i, 0, len) {
        cout << v[i] << (i == (int)len - 1 ? "\n" : " ");
    }
}

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} FASTIO;

//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------

signed main() {
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    rep(i, 0, N) {
        int id, k;
        cin >> id >> k;
        id--;
        rep(j, 0, k) {
            int u;
            cin >> u;
            u--;
            G[id].push_back(u);
        }
    }

    const auto inf = 1 << 30;
    vector<int> dist(N, inf);
    dist[0] = 0;
    queue<P> q;
    q.push(make_pair(0, 0));
    while (!q.empty()) {
        auto pi = q.front();
        q.pop();
        auto u = pi.first, cost = pi.second;
        for (auto& to : G[u]) {
            if (chmin(dist[to], cost + 1)) {
                q.push(make_pair(to, dist[to]));
            }
        }
    }

    rep(i, 0, N) {
        cout << i + 1 << " " << (dist[i] == inf ? -1 : dist[i]) << "\n";
    }
}

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;
}