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 <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#include <bitset>
#include <iomanip>
#include <map>
#include <queue>
#include <numeric>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::bitset;

#define rep(i,n)    for(int i=0; i<(int)n; i++)
#define reps(i,s,e) for(int i=s; i<(int)e; i++)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template<class T>
using Graph = vector<vector<T>>;

const int INT_BIG = ~(1<<28);

void printFixedDouble(double d, int fix) {
    cout << std::fixed;
    cout << std::setprecision(fix) << d << endl;
}

int main() {
    int nodeNum;
    cin >> nodeNum;
    Graph<int> nodes(nodeNum, vint());
    vint dist(nodeNum, -1);
    rep(i, nodeNum) {
        int u, k;
        cin >> u >> k;
        rep(j, k) {
            int v;
            cin >> v;
            nodes[u - 1].push_back(v - 1);
        }
    }

    std::queue<int> now;
    std::queue<int> next;
    next.push(0);
    int tmpdist = 0;
    while(!next.empty()) {
        now = next;
        next = std::queue<int>();
        while(!now.empty()) {
            int id = now.front();
            now.pop();
            if (dist[id] == -1) {
                dist[id] = tmpdist;
                for(auto& subid : nodes[id])
                next.push(subid);
            }
        }
        tmpdist++;
    }
    rep(i, nodeNum) {
        cout << i+1 << " " << dist[i] << endl;
    }
}

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

int n;
vector <int> G[101];
queue <int> Q;
int  d[100];
int  dis[101];


int visit(int x){
  int t,i;
  
  Q.push(x);
  dis[x] = 0;
  d[x] = 1;

  while(Q.empty() == false) {
    
    cout << "start Q" <<endl;

    t = Q.front();
    Q.pop();

    for(i=0; i<G[t].size();i++) {
      if(d[G[t][i]] == 0 ) {
	d[G[t][i]] = 1;
	dis[G[t][i]] = dis[t] + 1;

	Q.push(G[t][i]);
      }

    }
  }
}
    
int main() {
  
  int i,j,k,l,u,a,t;
  
  
  scanf("%d",&n);

  
  
  for(i=0;i<n;i++){
    d[i] = 0;
    dis[i] = -1;
    scanf("%d %d ",&k,&u);
    
    for(j=0;j<u;j++) {
      scanf("%d",&a);
      G[k-1].push_back(a-1);
      // printf("G[%d][%d] = 1\n",i,k-1);
    }
  }
  
  
  visit(0);

  

  for(i=0;i<n;i++) cout << i+1<<" " << dis[i] <<endl;



  return 0;

    
    
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int MAX_N = 150;
template<class Type> class Queue
{
    private:
        struct Node { Type num; Node *back; };
        Node *head, *tail;
        int sz;
    public:
        Queue() { head = tail = new Node; sz = 0; }
        void push(Type x)
        {
            Node *n = new Node;
            n->num = x;
            tail->back = n; tail = n;
            if(empty()) head = tail; sz++;
        }
        Type pop()
        {
            Type res = head->num;
            Node *_head = head;
            head = head->back;
            delete _head; sz--;
            return res;
        }
        Type front() { if(not empty()) return head->num; }
        bool empty() { return sz == 0; }
};
int Gragh[MAX_N][MAX_N];
int used[MAX_N];
void bfs(int u, int n)
{
    used[u] = 0;
    Queue<int> que;
    que.push(u);
    while(!que.empty())
    {
        int u = que.front(); que.pop();
        for(int v = 1; v <= n; v++)
        {
            if(Gragh[u][v] == -1 || used[v] != -1) continue;
            used[v] = used[u] + 1;
            que.push(v);
        } 
    }
}
void init(int n)
{
    for(int i = 0; i <= n; i++)
    {
        used[i] = -1;
        for(int j = 0; j <= n; j++) Gragh[i][j] = -1;
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    init(n);
    for(int i = 0; i < n; i++)
    {
        int u, k; cin >> u >> k;
        for(int j = 0; j < k; j++)
        {
            int v; cin >> v;
            Gragh[u][v] = 1;
        }
    }
    bfs(1, n);
    for(int u = 1; u <= n; u++) cout << u << " " << used[u] << endl;
    return 0;
}

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

struct node
{
  int name;
  std::vector<node*> neighbors;
  int distance{-1};
};

void solve(std::vector<node>& nodes)
{
  std::queue<node*> queue;
  auto& root = nodes.front();
  root.distance = 0;
  queue.push(&root);

  while (!queue.empty()) {
    auto* target = queue.front();
    queue.pop();

    for (auto* n : target->neighbors) {
      if (n->distance >= 0) continue;
      n->distance = target->distance + 1;
      queue.push(n);
    }
  }
}

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

  std::vector<node> nodes(n);
  for (int i = 0; i < n; ++i) {
    int u, k;
    std::cin >> u >> k;
    auto& node = nodes.at(u - 1);
    node.name = u;
    node.neighbors.resize(k);
    for (auto& nb : node.neighbors) {
      int v;
      std::cin >> v;
      nb = &nodes.at(v - 1);
    }
  }

  solve(nodes);

  for (const auto& node : nodes) {
    std::cout << node.name << ' ' << node.distance << std::endl;
  }
}