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<iostream>
#include<queue>
using namespace std;
#define MAX 100
#define NIL -1

int n, T[MAX+1][MAX+1], d[MAX+1];
string color[MAX+1];
queue<int> Q;

void search(int u){
  Q.push(u);
  while(!Q.empty()){
    u = Q.front();
    color[u] = "black";
    for ( int i = 1; i <= n; i++){
      if ( T[u][i] && color[i] == "white"){
        if ( d[u] != NIL){
          d[i] =  d[u] + 1;
          color[i] = "brown";
        }
        Q.push(i);
      }
    }
    Q.pop();
  }
}

int main(){
  cin >> n;

  //init
  for ( int i = 1; i <= n; i++){
    color[i] = "white";
    d[i] = NIL;
    for ( int j = 0; j <= n; j++){
      T[i][j] = 0;
    }
  }

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

  d[1] = 0;
  for ( int i = 1; i <= n; i++){
    search(i);
  }

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

  return 0;
}

C++: #line 1 "test/ALDS1_11_C.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=ja"
#line 1 "macro/macros.hpp"



#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
#define Yes "Yes"
#define No "No"
#define YES "YES"
#define NO "NO"
typedef long long ll;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << "{" <<p.first << ", " << p.second << "}";
  return os;
}
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = (ll)1e9+7;

const double PI = acos(-1.0);

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";


#line 1 "graph/template.hpp"


#line 4 "graph/template.hpp"

template<typename T = int>
struct edge{
    int to;
    T cost;
    int id;
    edge(int _to, T _cost = 1, int _id = -1) :to(_to), cost(_cost), id(_id) {}
};

template<class T>
class Graph {
  public:
    int N;
    vvec<edge<T>> G;
    Graph(int _N): N(_N),G(_N){
    }
    void add_Directed_edge(int from, int to, T cost = 1, int id = -1){
        G[from].push_back({to, cost, id});
    }
    void add_edge(int v1, int v2, T cost = 1, int id = -1){
        add_Directed_edge(v1, v2, cost, id);
        add_Directed_edge(v2, v1, cost, id);
    }
    //standard input
    void input(int M, int padding = -1, bool weighted = false, bool directed = false){
        rep(i,M){
            int a, b;
            cin >> a >> b;
            a += padding;
            b += padding;
            T c = T(1);
            if(weighted)cin >> c;
            if(directed)add_Directed_edge(a,b,c);
            else add_edge(a,b,c);
        }
    }
};

#line 1 "graph/shortest_path/bfs.hpp"


#line 5 "graph/shortest_path/bfs.hpp"

template<typename T>
class BFS : public Graph<T>{
  public:
    using Graph<T>::Graph;
    int N;
    vec<T> d;
    BFS(int _N):N(_N), Graph<T>::Graph(_N), d(_N,-1){
    }
    vec<T> bfs(int start = 0){
        auto edges = Graph<T>::G;
        queue<int> q;
        q.push(start);
        d[start] = 0;
        while(!q.empty()){
            int cur = q.front(); q.pop();
            for(auto nv: edges[cur]){
                if(d[nv.to] != -1)continue;
                d[nv.to] = d[cur] + 1;
                q.push(nv.to);
            }
        }
        return d;
    }
};

#line 5 "test/ALDS1_11_C.test.cpp"

int main(){
    int N;
    cin >> N;
    BFS<int> G(N);
    rep(i,N){
        int u, k;
        cin >> u >> k;
        u--;
        rep(j,k){
            int v;
            cin >> v;
            v--;
            G.add_Directed_edge(u, v);
        }
    }
    G.bfs();
    rep(i,N){
        cout << i + 1 << " " << G.d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
inline bool chmin(T& a, T b) {
   if (a > b) {
      a = b;
      return true;
   }
   return false;
}
template <class T>
inline bool chmax(T& a, T b) {
   if (a < b) {
      a = b;
      return true;
   }
   return false;
}
void hr() {
   cerr << "--------------" << endl;
}
const int INF = 1001001001;
const int MOD = 1000000007;

vector<int> to[100];
int main() {
   cin.tie(0);
   ios_base::sync_with_stdio(0);

   int n;
   cin >> n;
   rep(i, 0, n) {
      int u, k;
      cin >> u >> k;
      u--;
      rep(i, 0, k) {
         int v;
         cin >> v;
         v--;
         to[u].push_back(v);
      }
   }
   queue<int> que;
   vector<int> d(n, -1);
   que.push(0);
   d[0] = 0;
   while (que.size()) {
      int u = que.front();
      que.pop();
      for (auto v : to[u]) {
         if (d[v] < 0) {
            d[v] = d[u] + 1;
            que.push(v);
         }
      }
   }
   rep(i, 0, n) printf("%d %d\n", i + 1, d[i]);
   return 0;
}

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

static const int N = 100;
static const int INF = 100000000000;

int n, Adj[N][N];
int d[N];


void bfs(int s) {
	

	queue<int> q;
	q.push(s);
	
	for (int i=0; i< n;i++)d[i] = INF;
	d[s] = 0;
	int u;

	while (!q.empty()) {
		u = q.front(); q.pop();
		for(int v=0;v<n;v++){
			if (Adj[u][v] == 0) continue;
			if (d[v] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
			}
		}
	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((d[i] == INF) ? (-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++) {
			Adj[i][j] = 0;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++) {
			cin >> v;
			v--;
			Adj[u][v] = 1;
		}
	}
	
	bfs(0);
	return 0;
}