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++: #include<iostream>
#include<queue>
using namespace std;

static const int N = 100;
static const int INF = 100000000000;

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


void bfs(int s) {
	

	queue<int> q;
	q.push(s);
	
	for (int i=0; i< n;i++)d[i] = INF;
	d[s] = 0;
	int u;

	while (!q.empty()) {
		u = q.front(); q.pop();
		for(int v=0;v<n;v++){
			if (Adj[u][v] == 0) continue;
			if (d[v] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
			}
		}
	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((d[i] == INF) ? (-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++) {
			Adj[i][j] = 0;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++) {
			cin >> v;
			v--;
			Adj[u][v] = 1;
		}
	}
	
	bfs(0);
	return 0;
}

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

int G[110][110];
bool seen[110];
int d[110];
int N;

struct MyQueue{
    int num[110];
    int head = 0;
    int tail = 0;

    void enqueue( int n ){
        num[tail++] = n;
    }

    int dequeue(){
        return num[head++];
    }

    bool empty(){
        return head == tail;
    }
};

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

    MyQueue que;
    que.enqueue( 0 );
    seen[0] = 1;
    while( !que.empty() ){
        int u = que.dequeue();
        for( int v = 0; v < N; v++ ){
            if( G[u][v] && !seen[v] ){
                que.enqueue( v );
                d[v] = d[u] + 1;
                seen[v] = 1;
            }
        }
    }

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

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

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> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <iomanip>
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const int INF = 1000000000;
const int MOD = 1000000007;

typedef pair<int, int> P;
int myAbs(int num) {
    if (num > 0)
        return num;
    else
        return -num;
}

long long gcd(long long a, long long b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a * b / gcd(a, b);
}

using Graph = vector<vector<int>>;
//再帰とかグラフを使うときは予めNの最大値のグローバル配列を取っておく
int N;
bool E[101][101];
vector<bool> seen;
typedef pair<int, int> P;
int D[101];
//頂点状態を表すcolor[]において、0は未訪問、1は訪問中、2は完了を表す。
int color[101];
int find_time = 0;

void bfs(int start) {
    //全頂点を未訪問にして距離も初期化(INF)
    for (int i = 0; i < 101; i++) D[i] = -1;
    queue<int> Q;
    //最初の頂点をキューに入れる
    Q.push(start);
    int v;
    //最初の頂点は距離0
    D[start] = 0;
    //キューが空になるまで探索
    while (!Q.empty()) {
        v = Q.front();
        Q.pop();
        for (int i = 1; i <= N; i++) {
            //今訪問している頂点vから辺があるか&&すでに訪問済み(距離がINFではない)を確認
            if (!E[v][i]) continue;
            if (D[i] != -1) continue;
            //未訪問かつ辺があったなら元の頂点の距離に1を足す
            D[i] = D[v] + 1;
            //発見した頂点をキューに入れて再度探索
            Q.push(i);
        }
    }
    for (int i = 1; i <= N; i++) {
        cout << i << " " << D[i] << endl;
    }
}

int main() {
    cin >> N;
    int u, k,v;
    //頂点を1から入力(0ではなく)
    //辺の有無をbool E[][]で管理
    for (int i = 0; i < N; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> v;
            E[u][v] = true;
        }
    }
    //探索開始頂点を引数にして開始
    bfs(1);

}