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>
#define INFTY -1
using namespace std;



int a[124][124] = {{}};
int d[124] = {};
int n;
void bfs(int k){
  queue <int> q;
  q.push(k);
  int u;


  while(!q.empty()){
    u = q.front(); q.pop();
    for(int v = 0;v < n;v++){
      if(a[u][v] == 0) continue;
      if(d[v] == INFTY) {
        d[v] = d[u] + 1;
        q.push(v);
        cout << v << " " << d[v] << endl;
      }
    }

  }
}

int main(){

  int u,k,x;
  cin >> n;

d[0] = 0;
for(int i = 1; i < n;i++) d[i] = INFTY;

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

  bfs(0);

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

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;

void calc(const vector<vector<int> >& G, vector<int>& D,
                                        queue<int>& Q, int dim) {
    if (Q.empty()) return;
    queue<int> Q2;
    while (!Q.empty()) {
        int i = Q.front();
        for (int j = 1; j < G[i].size(); j++) {
            if (G[i][j] && D[j] == -1) {
                Q2.push(j);
                D[j] = dim;
            }
        }
        Q.pop();
    }
    calc(G, D, Q2, dim + 1);
}

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

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

    vector<int> D(n + 1, -1);

    queue<int> Q;
    Q.push(1);
    D[1] = 0;
    calc(G, D, Q, 1);

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

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<deque>

using namespace std;

int n;
int u[102], k[102], t[102][102];
queue<int>Q;
vector<int>V;
int color[102];
int d[102];

void bfs(int s){
  for(int i = 1; i <= n; ++i){
    if(u[i] != s){
      color[u[i]] = 0;
      d[u[i]] = 150;
    }
  }
  d[s] = 0;
  int pi = 0;
  Q.push(s);
  while(!Q.empty()){
    int m = Q.front();
    Q.pop();
    for(int i = 1; i <= k[m]; ++i){
      if(color[t[m][i]] == 0){
        color[t[m][i]] = 2;
        d[t[m][i]] = d[m] + 1;
        pi = t[m][i];
        Q.push(t[m][i]);
      }
    }
    color[m] = 1;
  }
  for(int i = 1; i <= n; ++i){
    if(d[i] == 150){
      cout << i << " " << -1 << endl;
    }else{
      cout << i << " " << d[i] << endl;
    }
  }
  
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; ++i){
    cin >> u[i] >> k[i];
    for(int j = 1; j <= k[i]; ++j){
      cin >> t[i][j];
    }
  }
  bfs(1);
}

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

C++: #include <iostream>
#include <queue>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int adj_matrix[101][101] = {0};
    int visit[101] = {0};
    queue<int> q;
    int dist_from_start[101] = {0};
    for (int i = 1; i <= n; i++)
    {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++)
        {
            int x;
            cin >> x;
            adj_matrix[u][x] = 1;
        }
    }

    q.push(1);
    visit[1] = 1;
    while (!q.empty())
    {
        int now = q.front();
        for (int i = 1; i <= n; i++)
        {
            if (adj_matrix[now][i] == 1 && visit[i] == 0)
            {
                visit[i] = 1;
                q.push(i);
                dist_from_start[i] = dist_from_start[now] + 1;
            }
        }
        q.pop();
    }

    for (int i = 1; i <= n; i++)
    {
        if (i == 1 || dist_from_start[i] > 0)
        {
            cout << i << " " << dist_from_start[i] << endl;
        }
        else
        {
            cout << i << " " << -1 << endl;
        }
    }

    return 0;
}