Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include <bits/stdc++.h>
       
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
       
       

struct Edge {
  int from, to;
  Edge(){};
  Edge(int from, int to) : from(from), to(to) {}

  void scanFrom(std::istream& strm, int base = 1) {
    strm >> from >> to;
    if (base == 1) --from, --to;
  }

  Edge reverse() const { return Edge(to, from); }
};

inline std::istream& operator>>(std::istream& strm, Edge& t) {
  t.scanFrom(strm);
  return strm;
}
       
       

template <typename T>
struct WeightedEdge {
  int from, to;
  T _weight;
  WeightedEdge(){};
  WeightedEdge(int from, int to, T weight)
      : from(from), to(to), _weight(weight) {}

  void scanFrom(std::istream& strm, int base = 1) {
    strm >> from >> to >> _weight;
    if (base == 1) --from, --to;
  }

  T weight() const { return _weight; }

  WeightedEdge reverse() const { return WeightedEdge(to, from, _weight); }
};

template <typename T>
inline std::istream& operator>>(std::istream& strm, WeightedEdge<T>& t) {
  t.scanFrom(strm);
  return strm;
}

template <typename Edge>
class Digraph {
  int _E, _V;
  vector<vector<Edge>> _adjacencyList;

 public:
  int E() const { return _E; }

  int V() const { return _V; }
  int size() const { return _V; }

  Digraph() : _V(0), _E(0){};
  Digraph(int V, int E = 0) : _V(V), _E(E) { _adjacencyList.resize(V); }

  void addEdge(const Edge& e) {
    _adjacencyList[e.from].push_back(e);
    ++_E;
  }

  void addUndirectedEdge(const Edge& e) {
    _adjacencyList[e.from].push_back(e);
    _adjacencyList[e.to].push_back(e.reverse());
    ++_E;
  }

  void addNode() {
    ++_V;
    _adjacencyList.push_back(vector<Edge>());
  }

  void scanFrom(std::istream& strm, int base = 1) {
    if (_V == 0) strm >> _V;
    if (_E == 0) strm >> _E;
    _adjacencyList.resize(_V);
    for (int i = 0; i < _E; ++i) {
      Edge e;
      e.scanFrom(cin, base);
      assert(0 <= e.from < _V);
      assert(0 <= e.to < _V);
      addEdge(e);
    }
  }

  vector<Edge> operator[](int u) const { return _adjacencyList[u]; }
  vector<vector<Edge>>& adjacencyList() { return _adjacencyList; }
};

template <typename Edge>
inline std::istream& operator>>(std::istream& strm, Digraph<Edge>& t) {
  t.scanFrom(strm);
  return strm;
}

class BFSShortestPath {
  const int INF = numeric_limits<int>::max();
  vector<int> _distTo;
  vector<int> prev;

 public:
  BFSShortestPath(const Digraph<Edge>& G, int s)
      : _distTo(G.V(), INF), prev(G.V(), -1) {
    queue<int> q;
    q.push(s);
    _distTo[s] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (Edge& e : G[u]) {
        if (_distTo[e.to] != INF) continue;
        _distTo[e.to] = _distTo[u] + 1;
        prev[e.to] = u;
        q.push(e.to);
      }
    }
  }

  int distTo(int u) { return _distTo[u]; }

  bool hasPathTo(int u) { return _distTo[u] != INF; }

  vector<int> pathTo(int u) {
    assert(hasPathTo(u));
    vector<int> path;
    while (u != -1) {
      path.push_back(u);
      u = prev[u];
    }
    return path;
  }
};

int main() {
  int n;
  cin >> n;
  Digraph<Edge> G(n);

  for (int i = 0; i < (int)(n); ++i) {
    int u, k;
    cin >> u >> k;
    --u;
    for (int j = 0; j < (int)(k); ++j) {
      int v;
      cin >> v;
      --v;
      G.addEdge(Edge(u, v));
    }
  }

  BFSShortestPath sp(G, 0);
  for (int i = 0; i < (int)(n); ++i) {
    cout << i + 1 << " " << (sp.hasPathTo(i) ? sp.distTo(i) : -1) << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace	std;
int main(){

  int n,g[100][100]={},tmp1,amo,tmp2;
  queue <int> q;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>tmp1>>amo;
    for(int j=0;j<amo;j++){
      cin>>tmp2;
      g[tmp1-1][tmp2-1]=1;
    }
  }
  int dis[100]={},flag[100]={1};
  for(int i=0;i<n;i++) if(i) dis[i]=-1;
  q.push(0);
  for(;!q.empty();){
    for(int i=0;i<n;i++) if(g[q.front()][i]&&flag[i]!=1){
        dis[i]=dis[q.front()]+1;
        flag[i]=1;
        q.push(i);
      }
    q.pop();
  }
  for(int i=0;i<n;i++) cout<<i+1<<" "<<dis[i]<<endl;
  return 0;

}

C++: #include "bits/stdc++.h"

using namespace std;

void Main() {
    int n;
    cin >> n;
    vector<set<int>> graph(n, set<int>());
    for (int i = 0; i < n; ++i) {
        int u, k;
        cin >> u >> k;
        --u;
        for (int j = 0; j < k; ++j) {
            int v;
            cin >> v;
            --v;
            graph[u].insert(v);
        }
    }

    queue<pair<int, int>> q;
    vector<int> distances(n, -1);
    q.push(make_pair(0, 0));
    distances[0] = 0;
    while (!q.empty()) {
        pair<int, int> curr_dist = q.front();
        q.pop();
        int curr = curr_dist.first;
        int dist = curr_dist.second;
        const set<int>& nexts = graph.at(curr);
        for (int e : nexts) {
            if (distances[e] < 0) {
                q.push(make_pair(e, dist + 1));
                distances[e] = dist + 1;
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << i + 1 << " " << distances[i] << endl;
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

C++: #include<iostream>
#include<queue>
#define black 2
#define white 0
#define gray 1
using namespace std;
int matrix[101][101];
int n;
int temp;
int d[101]={-1};
int color[101]={white};
int main(){
    for(int i=0;i<101;i++){
        for(int j=0;j<101;j++)
            matrix[i][j]=0;
    }
    cin>>n;
    int x,y;
    for(int i=0;i<n;i++){
        cin>>x>>temp;
        for(int j=0;j<temp;j++){
            cin>>y;
            matrix[x][y]=1;
        }
    }
    queue <int> q;
    q.push(1);
    for(int i=0;i<=n;i++)
      d[i]=-1;
    color[1]=gray;
    d[1]=0;

    while(!q.empty()){
        temp=q.front();
        for(int i=1;i<=n;i++){
            if(matrix[temp][i]==1&&color[i]==white){
                q.push(i);
                color[i]=gray;
                d[i]=d[temp]+1;
            }
        }
        color[temp]=black;
        q.pop();
    }
    for(int i=1;i<=n;i++)
        cout<<i<<" "<<d[i]<<endl;
    return 0;
}