Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (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] = INFTY;
  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]!=INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    cout << i+1 << " " << ((d[i]== INFTY)?(-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;

#define MAX 100
#define INF -1

int n;
int G[MAX][MAX];
int dist[MAX];

void bfs(){
  bool used[MAX];
  fill(dist,dist+n,INF);
  fill(used,used+n,false);
  dist[0] = 0;
  used[0] = true;

  queue<int> Q;
  Q.push(0);

  while(!Q.empty()){
    int v = Q.front(); Q.pop();

    for(int i = 0 ; i < n ; i++){
      if(!used[i] && G[v][i]){
	used[i] = true;
	dist[i] = dist[v] + 1;
	Q.push(i);
      }
    }
  }
}

int main(){
  cin >> n;

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

  bfs();

  for(int i = 0 ; i < n ; i++){
    cout << i+1 << " " << dist[i] << endl;
  }

  return 0;
}

C++: #include "bits/stdc++.h"
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define IOS std::cin.tie(0),std::ios::sync_with_stdio(false)

namespace idaten {

	using namespace std;
	using ll = int_fast64_t;
	using ull = uint_fast64_t;
	using P = std::pair<int64_t, int64_t>;
	template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
	template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
	template<class Type>struct is_vector : std::false_type {};
	template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
	template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
		if (v.empty())return out;
		constexpr bool is_vector_v = is_vector<T>::value;
		if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
		else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
		return out;
	}
	inline void put() {}
	template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
	template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
	inline void putn() {}
	template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

	void solve() {
		IOS;
		int n;
		cin >> n;
		vector<vector<int>> path(n);
		REP(i, n) {
			int u, k;
			cin >> u >> k;
			u--;
			REP(j, k) {
				int v;
				cin >> v;
				v--;
				path[u].push_back(v);
			}
		}
		queue<int> q;
		vector<int> d(n, -1);
		int s = 0;
		d[s] = 0;
		q.push(s);
		while (!q.empty()) {
			int p = q.front();
			q.pop();
			REP(i, path[p].size()) {
				int t = path[p][i];
				if (d[t] < 0) {
					d[t] = d[p] + 1;
					q.push(t);
				}
			}
		}
		REP(i, n) {
			put(i + 1, d[i]);
		}

	}
} // namespace idaten

int main() {
	idaten::solve();
	return 0;
}

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

const int INF = 1e9;

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

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

  queue<int> q;
  vector<int> d(n, -1);
  d[0] = 0;
  q.push(0);
  while ( !q.empty() ) {
    int p = q.front(); q.pop();
    for ( int i = 0; i < n; i++ ) {
      if ( G[p][i] && d[i] == -1 ) {
	q.push(i);
	d[i] = d[p]+1;
      }
    }
  }

  for ( int i = 0; i < n; i++ ) {
    cout << i+1 << " " << d[i] << endl;
  }
  
  return 0;
}