Go: package main

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

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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<k;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;
static const int N = 100;
static const int INF = (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] = INF;
	}
	d[s] = 0;
	int u;
	while (!q.empty()) {
		u = q.front();
		q.pop();
		for (int v = 0; v < n; v++) {
			if (M[u][v] == 1 && d[v] == INF) {
				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 argc, char *argv[]) {
	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 <bits/stdc++.h>
using namespace std;
bool A[105][105],V[105]={0};
int d[105],n;
queue<int> B;

void bfs(){
	B.push(1);
	d[1] = 0;
	V[1] = 1;
	while(!B.empty()){
		int v = B.front();
		B.pop();
		for(int i=1;i<=n;i++)
			if(A[v][i] && !V[i]){
				d[i] = d[v]+1;
				V[i] = 1;
				B.push(i);
			} 
	}
}
int main()
{	
	memset(A,0,sizeof(A));
	int v,u,du;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>v>>du;
		while(du--)
			cin>>u,
			A[v][u] = 1;
	}
	bfs();
	for(int i=1;i<=n;i++)
		if(!V[i]) d[i] = -1;
	for(int i=1;i<=n;i++)
		printf("%d %d\n",i,d[i]);
	return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

// Acknowledgement: Special thanks to kyomukyomupurin, who developed this
// template.
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
  int n = 0;
  for (auto e : vec) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& st) {
  int n = 0;
  for (auto e : st) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) {
  int n = 0;
  for (auto e : mp) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
  for (T& e : vec) is >> e;
  return is;
}

#ifdef LOCAL
#define debug(...) \
  std::cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\n'; }

template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0) std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}
using namespace std;
using int64 = long long;

void bfs(int start_node_id, const vector<vector<int>>& g,
         vector<int>& visit_status, vector<int>& distance, vector<int>& parent,
         int col = 1) {
  int node_id;
  queue<int> q;
  visit_status[start_node_id] = col;
  distance[start_node_id] = 0;
  q.push(start_node_id);
  while (!q.empty()) {
    node_id = q.front();
    q.pop();
    for (int i = 0; i < (int)g[node_id].size(); i++) {
      int next_node_id = g[node_id][i];
      if (visit_status[next_node_id] == 0) {
        q.push(next_node_id);
        visit_status[next_node_id] = col;
        distance[next_node_id] = distance[node_id] + 1;
        parent[next_node_id] = node_id;
      }
    }
    visit_status[node_id] = col;
  }
}

int main() {
  int n, k, u, v;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      g[i].push_back(v - 1);
    }
  }
  debug(g);
  vector<int> visit_status(n), distance(n, -1), parent(n);
  bfs(0, g, visit_status, distance, parent);
  for (int i = 0; i < n; i++) {
    cout << i + 1 << ' ' << distance[i] << endl;
  }
  return 0;
}

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

int AdjM[101][101]={0};
int D[101];

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

void bfs(int s){
    //cerr<<"bfs root "<<s<<"\n";
    queue<int> Q;
    Q.push(s);
    D[s]=0;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        //cerr<<"visiting "<<cur<<" "<<D[cur]<<"\n";
        for(int i=1;i<101;++i){
            if(AdjM[cur][i]!=0 && D[i]==-1){
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
}

int main(){
    Fill(D,-1);
    int n;
    cin>>n;
    int id,num,ix;
    for(int i=0;i<n;++i){
        cin>>id>>num;
        for(int j=1;j<=num;++j){
            cin>>ix;
            AdjM[id][ix]=1;
        }
    }

    bfs(1);

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