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

static const int MAX_NUM = 101;
static const int NO_VISIT  = 0;
static const int END_VISIT = 2;

void BFS(int *matrix, int num)
{
    int i;
    int state[MAX_NUM]    = {0};
    int distance[MAX_NUM];
    queue<int> Q;
    int target;
    int dist = 0;
    int *tmp;
    bool first;

    for (i = 1; i <= num; i++) {
        distance[i] = -1;
    }

    Q.push(1);
    state[1] = END_VISIT;
    distance[1] = 0;

    while (Q.empty() == false) {
        target = Q.front();
        Q.pop();
        tmp = matrix + target * MAX_NUM;
        first = true;
        for (i = 1; i <= num; i++) {
            if ((*(tmp + i) == 1) && (state[i] == NO_VISIT)) {
                distance[i] = distance[target] + 1;
                Q.push(i);
                state[i] = END_VISIT;
            }
        }
    }

    for (i = 1; i <= num; i++) {
        printf("%d %d\n", i, distance[i]);
    }

    return;
}

int main(void)
{
    int i, j;
    int num;
    int elem;
    int nume;
    int tmp;
    int matrix[MAX_NUM][MAX_NUM];

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        scanf("%d %d", &elem, &nume);
        for (j = 0; j < nume; j++) {
            scanf("%d", &tmp);
            matrix[elem][tmp] = 1;
        }
    }

    BFS(&matrix[0][0], num);

    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
using namespace std;

const int N = 100;
const int INF = (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] = INF;

    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] != 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++) {
            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 INF=(1<<21);
int n, MA[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(MA[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++)
      MA[i][j]=0;
  }

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

  Bfs(0);

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   for(int i=0;i<(n);++i)
#define SORT(c)    sort((c).begin(),(c).end())
#define CLR(a)     memset((&a), 0 ,sizeof(a))//clear memory
#define dump(x)    cerr << #x << " = " << (x) << endl;//debug
#define debug(x)   cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define EPS        1e-10//sample:if((double)>=(double)+EPS)
//cin,cout release
//std::cin.tie(0);
//ios::sync_with_stdio(false);

using namespace std;

int n,v[100+1][100+1]={},d[1000]={},color[1000]={};
int q[1000]={},head=0,tail=0;
void bfs(int s){
  rep(i,n){
    if(v[s][i]==0&&s!=i){
      color[s]=0;
      d[i]=INT_MAX;
    }
  }
  color[s]=1;
  d[s]=0;
  head=tail=0;
  q[tail++]=s;
  while(head!=tail){
    int u=q[head++];
    rep(i,n){
      if(v[u][i]==0)continue;
      if(color[i]==0){
	color[i]=1;
	d[i]=d[u]+1;
	q[tail++]=i;
      }
    }
    color[u]=2;
  }
}
int main(){
  cin>>n;
  int u,k,in;
  rep(i,n){
    cin>>u>>k;
    rep(j,k){
      cin>>in;
      v[u-1][in-1]=1;
    }
  }
  //rep(i,n)
  bfs(0);
  rep(i,n){
    cout<<i+1<<" ";
    if(d[i]==INT_MAX)cout<<-1;
    else cout<<d[i];
    cout<<endl;
  }
  return 0;
}

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

using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int GREY = 1;
static const int BLACK = 2;

int n, m[N][N];
bool visited[N];
int d[N], dist;


void bfs(int i){
	queue<int> q; q.push(i);
	visited[i] = true;

	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int v = 0; v < n; ++v) {
			if (!visited[v] && m[u][v]){
				d[v] = d[u] + 1;
				visited[v] = true;
				q.push(v);
			}
		}
	}
}


void search(){
	for (int i = 0; i < n; ++i) {
		d[i] = -1;
		visited[i] = false;
	}
	d[0] = 0;
	for (int i = 0; i < n; ++i) {
		if (!visited[i]) bfs(i);
	}
	for (int i = 0; i < n; ++i)	cout << i + 1 << " " << d[i] << endl;
}


int main()
{
	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) {
		int u, d; cin >> u >> d;
		for (int j = 0; j < d; ++j) {
			int v; cin >> v;
			m[u - 1][v - 1] = 1;
		}
	}

	search();

	return 0;
}