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 <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 100
#define Black 1
#define White 0

int f[MAX];
int adj[MAX][MAX];
int n;

void bfs(int r){
    rep(i,n)f[i]=-1;
    queue<int> Q;
    Q.push(r);
    f[r]=0;
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        rep(i,n){
            if(adj[u][i]==1&&f[i]==-1){
                f[i]=f[u]+1;
                Q.push(i);
            }
        }
    }
    rep(i,n){
        printf("%d %d\n",i+1,f[i]);
    }
}

int main(){
    int id,k,node;
    cin>>n;
    rep(i,n)rep(j,n)adj[i][j]=0;
    rep(i,n){
        scanf("%d %d",&id,&k);
        id--;
        rep(j,k){
            scanf("%d",&node);
            node--;
            adj[id][node]=1;
        }
    }
    bfs(0);
    return 0;
}

C++: #include <bits/stdc++.h>
#define INF 10000000000
using namespace std;

struct edge {int to, cost;};
//=============dijkstra============================
vector<long long> dijkstra(int s, int V, vector<vector<edge>> G)
{
  vector<long long> d(V, INF);
  typedef pair<long long, int> P;// first は最短距離　second は頂点の番号
  priority_queue<P, vector<P>, greater<P>> que;
  d.at(s) = 0;
  que.push(P(0, s));
  while (!que.empty())
  {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d.at(v) < p.first) continue;
    for (int i = 0; i < G.at(v).size(); ++i)
    {
      edge e = G.at(v).at(i);
      if (d.at(e.to) > d.at(v) + e.cost)
      {
        d.at(e.to) = d.at(v) + e.cost;
        que.push(P(d.at(e.to), e.to));
      }
    }
  }
  return d;
}
//=================================================


int main()
{
  int n;
  cin >> n;
  vector<vector<edge>> G(n, vector<edge>(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.at(u - 1).push_back({v - 1, 1});
    }
  }
  vector<long long> ans = dijkstra(0, n, G);
  for (int i = 0; i < n; ++i)
  {
    if (ans.at(i) == INF)
    {
      cout << i + 1 << ' ' << -1 << endl;
    }
    else
    {
      cout << i + 1 << ' ' << ans.at(i) << endl;
    }
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>

using namespace std;

const int MAX_N = 101;
const int DUMMY = 10000;

int N, u, k, v = 0;

vector<int>V [MAX_N];
int d [MAX_N];
bool visited [MAX_N];

bool bfs(int u, int depth)
{
    //if (V[u].size() == 0) return false;
    
    // for (int i = 0; i < V[u].size(); ++i)
    //     cout << V[u][i] << ' ';
    // cout << endl;
    
    for (int i = 0; i < V[u].size(); ++i)
        bfs(V[u][i] - 1, depth + 1);
    
    d[u] = min(d[u], depth);
    return true;
}

int main()
{
    //initialize
    for (int i = 0; i < MAX_N; ++i) {
        d[i]   = DUMMY;
        visited[i] = false;
    }
    
    //input
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) {
      scanf("%d %d", &u, &k);
      for (int j = 0; j < k; ++j) {
          scanf("%d", &v);
          V[i].push_back(v);
      }
      sort(V[i].begin(), V[i].end());
    }

    //serach
    bfs(1-1, 0);
    
    //output
    for (int i = 0; i < N; ++i) {
      if(d[i] == DUMMY) cout << i+1 << ' ' << -1 << endl;
      else cout << i+1 << ' ' << d[i] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
  const char* dlm = "";
  for(const auto& x:v) cout << exchange(dlm, "\t") << x;
  return os;
}
struct Graph{
  // size_t time;
  vector<vector<size_t>> v;
  // vector<size_t> d,f;
  vector<int32_t> d;
  // Graph(size_t n): time(1), v(n), d(n,0), f(n) {}
  Graph(size_t n): v(n), d(n,-1) {}
  // void dfs(size_t i){
  //   d[i]=time++;
  //   for(const auto& x:v[i]) if(d[x-1]==0) dfs(x-1);
  //   f[i]=time++;
  // }
  void bfs(size_t n){
    d[n]=0;
    deque<size_t> que(1,n);
    while(!que.empty()){
      size_t x=que.front(); que.pop_front();
      for(const auto& y:v[x]){
        if(d[y-1]==-1){
          d[y-1]=d[x]+1;
          que.push_back(y-1);
        }
      }
    }
  }
};
ostream& operator<<(ostream& os, const Graph& x){
  for(size_t i=0; i<x.v.size(); ++i)
    // cout<<(i+1)<<' '<<x.d[i]<<' '<<x.f[i]<<'\n';
    cout<<(i+1)<<' '<<x.d[i]<<'\n';
  return os;
}
int32_t main(){
  size_t n; cin>>n;
  Graph graph(n);
  for(size_t i=0; i<n; ++i){
    size_t id,k; cin>>id>>k;
    graph.v[id-1].resize(k); for(auto&& x:graph.v[id-1]) cin>>x;
  }
  graph.bfs(0);
  cout<<graph;
}