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 <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;
vector<int> d[120];
int dist[120];
bool visit[120];
void bfs(){
	queue<pii> que;
	que.push(mp(1,0));
	while(!que.empty()){
		pii p =que.front();
		que.pop();
		int pos =p.first;
		int cnt =p.second;
		if(visit[pos])continue;
		//cout <<pos<<" "<<cnt<<endl;
		visit[pos]=true;
		dist[pos]=cnt;
		for(int i =0;i <d[pos].size();i++){
			if(!visit[d[pos][i]]){
				que.push(mp(d[pos][i],cnt+1));
			}
		}
	}
}

int main(){
	cin>>n;
	memset(dist, -1, sizeof(dist));
	memset(visit, false, sizeof(false));
	for(int i =0;i < n;i++){
		int u,k;
		cin>>u>>k;
		for(int j = 0;j <k;j++){
			int v;
			cin>>v;
			d[u].push_back(v);
		}
	}
	bfs();
	for(int i =1;i <=n;i++){
		cout <<i<<" "<<dist[i]<<endl;
	}
	return 0;
}

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

int main()
{
    int D[101];
    queue<int> Q;
    int n,i,j,u,k;
    for(i=0;i<101;i++)
        D[i]=0;
    cin >> n;
    int Adj[n][n];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            Adj[i][j]=0;
    for(i=0;i<n;i++)
    {
        cin >> u >> k;
        int v[k];
        for(j=0;j<k;j++)
        {
            cin >> v[j];
            Adj[u-1][v[j]-1]=1;
        }
    }
    D[0]=0;
    Q.push(0);
    while(!Q.empty())
    {
        int cur = Q.front();
        Q.pop();
        for(i=0;i<n;i++)
        {
            if(Adj[cur][i]==1&&D[i]==0)
            {
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    cout << 1 << ' ' << D[i] << endl;
    for(i=1;i<n;i++)
    {
        if(D[i]==0)
            cout << i+1 << ' ' << -1 << endl;
        else    cout << i+1 << ' ' << D[i] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
// Welcome to my source code!

const int INF = 1e9;

int n;
vector<int> g[105];
int d[105];

void bfs() {
    queue<int> que;
    que.push(0);
    d[0] = 0;
    while (!que.empty()) {
        int u = que.front(); que.pop();
        for (auto v : g[u]) {
            int t = d[v];
            d[v] = min(d[v], d[u] + 1);
            if (t != d[v]) que.push(v);
        }
    }
}

int main() {
    cin >> n;
    for (int i = 0, u, k; i < n; i++) {
        cin >> u >> k;
        u--;
        for (int i = 0, v; i < k; i++) {
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    for (int i = 0; i < n; i++) d[i] = INF;
    bfs();
    for (int i = 0; i < n; i++) {
        cout << i + 1 << ' ';
        if (d[i] == INF) cout << -1;
        else cout << d[i];
        cout << endl;
    }
}

C++: #include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <vector>
#include <queue>
using namespace std;

#define DEBUG (1)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG


struct Vertex {
  Vertex() :
    id_(-1),
    visited_(0),
    distance_(-1),
    adjNum_(-1) { }

  int id_;
  bool visited_;
  int distance_;
  int adjNum_;
  unique_ptr<int[]> adjIds_;

  void set(int id, vector<int> ids);
  void printInfo();
  void printDistance();
};


void Vertex::set(int id, vector<int> ids)
{
  id_ = id;
  adjNum_ = ids.size();

  if (adjNum_ == 0)
    return;

  adjIds_ = unique_ptr<int[]>(new int[adjNum_]);

  for (int i = 0; i < adjNum_; i++)
    adjIds_[i] = ids[i];
}


void Vertex::printInfo()
{
  DPRINTF("%d %d", id_, adjNum_);
  for (int i = 0; i < adjNum_; i++)
    printf(" %d", adjIds_[i]);
  cout << endl;
}


void Vertex::printDistance()
{
  printf("%d %d\n", id_, distance_);
}


void bfs(Vertex vertices[])
{
  const int startId = 1;
  queue<int> idQueue;

  idQueue.push(startId);

  int distance = 0;

  while (!idQueue.empty()) {

    DPRINTF("queue size %lu\n", idQueue.size());

    const size_t numOfThisDist = idQueue.size();

    for (size_t i = 0; i < numOfThisDist; i++)
    {
      int id = idQueue.front();
      idQueue.pop();

      DPRINTF("%zu  id %d\n", i, id);

      Vertex &vtx = vertices[id];

      if (vtx.visited_)
        continue;

      vtx.visited_ = true;
      vtx.distance_ = distance;

      for (int j = 0; j < vtx.adjNum_; j++)
        idQueue.push(vtx.adjIds_[j]);

    }

    distance++;
  }
}


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

  DPRINTF("%d\n", n);

  // do not use [0], it's empty
  Vertex vertices[n+1];

  for (int i = 1; i <= n; i++)
  {
    int node, adjNum;

    cin >> node >> adjNum;

    vector<int> adjs;

    for (int j = 0; j < adjNum; j++)
    {
      int v;
      cin >> v;
      adjs.push_back(v);
    }

    vertices[i].set(node, adjs);

    cin.ignore();

  }

#ifdef DEBUG
  for (int i = 1; i <= n; i++)
    vertices[i].printInfo();
#endif // DEBUG

  bfs(vertices);

  for (int i = 1; i <= n; i++)
    vertices[i].printDistance();

  return 0;

}