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 <vector>
#include <queue>
#include <cstring>
#define INFTY 1000000000
using namespace std;
bool list[100][100];
int ti[100];
int n;

void bfs(int now){
	queue<int> next;
	next.push(now);
	while(next.size()!=0){
		now=next.front();	next.pop();
		for(int i=0;i<n;i++){
			if(list[now][i]&&ti[i]==INFTY){
				ti[i]=ti[now]+1;
				next.push(i);
			}
		}
	}
}

int main(){
	for(int i=0;i<100;i++){
		for(int j=0;j<100;j++)	list[i][j]=false;
		ti[i]=INFTY;
	}
	cin>>n;
	int id,num,ne;
	for(int i=0;i<n;i++){
		cin>>id>>num;
		id--;
		for(int j=0;j<num;j++){
			cin>>ne;
			list[id][ne-1]=true;
		}
	}
	for(int i=0;i<n;i++){
		if(ti[i]==INFTY){
			ti[i]=0;
			bfs(i);
		}
	}
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<((ti[i]==INFTY)?(-1):ti[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()){
    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<k;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;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int>to[105];
int main(){
  int n;
  cin>>n;
  rep(i,n){
    int u,k;
    cin>>u>>k;
    --u;
    rep(j,k){
      int v;
      cin>>v;
      --v;
      to[u].push_back(v);
      //to[v].push_back(u);
    }
  }
  queue<int>q;
  vector<int>dist(n,-1);
  dist[0]=0;
  q.push(0);
  while(!q.empty()){
    int v=q.front();q.pop();
    for(int u:to[v]){
      if(dist[u]!=-1)continue;
      dist[u]=dist[v]+1;
      q.push(u);
    }
  }
  rep(i,n)cout<<i+1<<" "<<dist[i]<<endl;
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#define INF 999999999
using namespace std;

int res[101];
int G[101][101] = {0};

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

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

    Q.push(0);
    res[0] = 0;

    while (! Q.empty()) {
        u = Q.front();
        for (int i = 0; i < n; i++) {
            if (G[u][i]) {
                Q.push(i);
                res[i] = std::min(res[i], res[u] + 1);
                G[u][i] = 0;
            }
        }
        Q.pop();
    }

    for (int i = 0; i < n; i++) {
        if (res[i] != INF) std::cout << i + 1 << " " << res[i] << "\n";
        else std::cout << i + 1 << " -1\n";
    }

    return 0;
}

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

const int MAX = 105;
int is_visited[MAX];
int dist[MAX];
int am[MAX][MAX];
queue<int> Q;
int t, n;
int ns[MAX];

int next(int u) {
  for (int i = ns[u]; i < n; i++) {
    ns[u] = i + 1;
    if (am[u][i] == 1) {
      return i;
    }
  }
  return -1;
}

void bfs_visit(int r) {
  int current = r;

  dist[current] = 0;
  is_visited[r] = 1;
  for (int i = 0; i < n; i++) {
    if (am[current][i] == 1) {
      Q.push(i);
      dist[i] = dist[current] + 1;
      is_visited[i] = 1;
    }
  }

  while (!Q.empty()) {
    current = Q.front();
    Q.pop();
    for (int i = 0; i < n; i++) {
      if (am[current][i] == 1 && is_visited[i] == 0) {
        Q.push(i);
        dist[i] = dist[current] + 1;
        is_visited[i] = 1;
      }
    }
  }
}

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

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

  bfs_visit(0);

  for (int i = 0; i < n; i++) {
    if (dist[i] == MAX) {
      dist[i] = -1;
    }
    cout << i+1 << " " << dist[i] << endl;
  }
}

C++: #define N 1000
#define M -1
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int m[N][N] = {};
int d[N] = {};
int n;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++)d[i] = M;
	int j;
	for (int i = 0; i < n; i++) {
		int k;
		int u;
		int v;
		cin >> u;
		cin >> k;
		for (j = 0; j < k; j++) {
			cin >> v;
			m[u][v] = 1;
		}
	}
	queue<int> q;
	q.push(1);
	d[1] = 0;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		if (d[u] != M) {
			for (int i = 1; i <= n; i++) {
				if (m[u][i]) {
					if (d[i] == M) {
						d[i] = d[u] + 1;
						q.push(i);
						//cout << "a" << endl;
					}
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}