Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

int head, tail;
int Q[100000];
int n, d[110];
bool a[110][110] = {};

void enqueue(int x){
  
  Q[tail] = x;

  if(tail + 1 == 100000) tail = 0;
  else tail++;
}

int dequeue(){
  
  int x = Q[head];

  if(head + 1 == 100000) head = 0;
  else head++;

  return x;
}

void bfs(int u){
  
  // queue<int> Q;
  
  enqueue(u);
  d[u] = 0;
  
  while(head != tail){
    int p = Q[head];
    dequeue();
    
    for(int i = 2;i <= n;i++){
      if(a[p][i] && d[i] == 0){
        enqueue(i);
        d[i] = d[p] + 1;
      }
    }
  }
}

int main(){
  
  int u, k, v;
  
  cin >> n;
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> v;
      a[u][v] = true;
    }
  }
  
  bfs(1);
  
  for(int i = 1;i <= n;i++){
    if(d[i] || i == 1) cout << i << " " << d[i] << endl;
    else cout << i << " " << -1 << endl;
  }
  
  return 0;
}

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 <vector>
#include <queue>
#include <algorithm>
using namespace std;

vector<int> G[102];
int d[102];

int main()
{
    int n,u,k,v;
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> u >> k;
        for(int j = 0; j < k; ++j){
            cin >> v;
            G[u].push_back(v);
        }
    }
    
    fill(d,d + n + 1, -1);
    
    queue<int> que;
    que.push(1);
    d[1] = 0;
    while(!que.empty()){
        int p = que.front(); que.pop();
        for(vector<int>::iterator it = G[p].begin(); it != G[p].end(); ++it){
            if(d[*it] == -1){
                d[*it] = d[p] + 1;
                que.push(*it);
            }
        }
    }
    
    for(int i = 1; i <= n; ++i){
        cout << i << ' ' << d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int adj_matrix[N_MAX+1][N_MAX+1];
array<int,N_MAX+1> depth;

int n;

void bfs(queue<int>& q, const int d) {
    if (q.empty()) { return; }
    queue<int> next_q;
    while (!q.empty()) {
        int node = q.front();
        if (depth[node] < 0) {
            depth[node] = d;
            FOR(i,1,n+1) {
                if (adj_matrix[node][i] && depth[i] < 0) {
                    next_q.push(i);
                }
            }
        }
        q.pop();
    }
    bfs(next_q, d+1);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;

    int id, k, v;
    FOR(i,0,n) {
        cin >> id;
        cin >> k;
        FOR(j,0,k) {
            cin >> v;
            adj_matrix[id][v] = 1;
        }
    }

    depth.fill(-1);

    queue<int> q;
    q.push(1);
    bfs(q, 0);

    FOR(i,1,n+1) {
        cout << i << " " << depth[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <string>

using namespace std;

int minDistance(vector<vector<int>>& edge, int st, int en) {
	size_t numNode = edge.size();
	vector<bool> passed(numNode, false);
	queue<int> v;
	queue<int> d;

	v.push(st);
	d.push(0);
	passed[st] = true;

	while(!v.empty()) {

		int nowV = v.front(); v.pop();
		int nowD = d.front(); d.pop();

		if(nowV == en) {
			return nowD;
		}

		for(auto nextV : edge[nowV]) {
			if(passed[nextV]) {
				continue;
			}

			v.push(nextV);
			d.push(nowD + 1);
			passed[nextV] = true;
		}

	}

	return -1;
}

int main() {

	int n;
	cin >> n;

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

	for(int i = 0; i < n; i++) {
		cout << i + 1 << " " << minDistance(edge, 0, i) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

static const int MAX=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int INF=-1;
int n,adj[MAX+1][MAX+1];
int color[MAX+1],d[MAX+1];
queue<int> Q;

void bfs(){
  for(int i=1;i<=n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }
  d[1]=0;
  color[1]=GRAY;
  Q.push(1);
  while(!Q.empty()){
    int u=Q.front(); Q.pop();
    for(int i=1;i<=n;i++){
      if(adj[u][i] && color[i]==WHITE){
        color[i]=GRAY;
        d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BLACK;
  }
}

int main(){
  cin >> n;
  for(int i=1;i<=n;i++){
    int u,k; cin >> u >> k;
    for(int i=1;i<=n;i++) adj[u][i]=0;
    for(int i=0;i<k;i++){
      int v; cin >> v;
      adj[u][v]=1;
    }
  }
  bfs();
  for(int id=1;id<=n;id++)
    printf("%d %d\n",id,d[id]);
}