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 <vector>
#include <unordered_map>
#include <queue>
//#include "../cxx-prettyprint/prettyprint.hpp"


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

    std::unordered_map<int, std::vector<int>> Adj(n);

    for (int i = 0; i < n; i ++) {
        int u, k;
        std::cin >> u >> k;
        std::vector<int> V(k);
        for (int& v : V) {
            std::cin >> v;
        }
        Adj[u] = V;
    }

    std::set<int> nodes_unvisited;
    std::unordered_map<int, int> distances;

    for (int i = 1; i <= n; i ++) {
        nodes_unvisited.insert(i);
    }

    nodes_unvisited.erase(1);
    distances[1] = 0;
    std::queue<int> queue;
    queue.push(1);

    while (!queue.empty()) {
        int u = queue.front();
        queue.pop();
        for (int v : Adj[u]) {
            if (nodes_unvisited.find(v) != nodes_unvisited.end()) {
                nodes_unvisited.erase(v);
                distances[v] = distances[u] + 1;
                queue.push(v);
            }
        }
    }

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

C++: #include<cstdio>
#include<algorithm>

using namespace std;

class Graph{
    int n;
    int **G;
    int *seen;
    int *todo;
    int *next;
    int *dist;
    const int MAX = 101;
    public: Graph(int _n){
        n = _n;
        G = new int*[n+1];
        seen = new int[n+1];
        todo = new int[n+1];
        next = new int[n+1];
        dist= new int[n+1];
        for(int i = 0;i <=n;++i){
            G[i] = new int[n+1];
            seen[i] = 0;
            todo[i] = 0;
            next[i] = 0;
            dist[i] = MAX;
        }
        for(int i = 0;i <= n;++i){
            for(int j = 0;j <= n;++j){
                G[i][j] = 0;
            }
        }
    }
    
    public: void BuildMatrix(){
        int u,k,v;
        for(int i = 0;i < n;++i){
            scanf("%d %d",&u,&k);
            for(int j = 0;j < k;++j){
                scanf("%d",&v);
                G[u][v] = 1;
            }
        }
    }
    
    public: void PrintMatrix(){
        for(int i = 1;i <= n;++i){
            for(int j = 1;j <n;++j){
                printf("%d ",G[i][j]);
            }
            printf("%d\n",G[i][n]);
        }
    }
    
    
   public: void DoBFS(){
        dist[1] = 0;
        todo[1] = 1;
        while(!isEmpty(todo)){
            for(int i = 1;i <= n;++i){
                if(todo[i] == 1) BFS(i,dist[i]);
            }
            for(int i = 1;i <= n;++i) swap(todo[i],next[i]);
        }
        for(int i = 1;i <= n;++i) printf("%d %d\n",i,(dist[i] != MAX) ? dist[i]:-1);
    }
    void BFS(int s,int cnt){
        cnt++;
        todo[s] = 0;
        seen[s] = 1;
        for(int i = 1;i <= n;++i){
            if(G[s][i]  == 1){
                dist[i] = std::min(dist[i],cnt);
                if(seen[i] != 1) next[i] = 1;
            }
        }
    }
    
    bool isEmpty(int* l){
        for(int i = 1;i <= n;++i){
            if(l[i] != 0) return false;
        }
        return true;
    }
    
};

int main(){
    int n;
    scanf("%d",&n);
    Graph G(n);
    G.BuildMatrix();
    G.DoBFS();
}

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

using namespase 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];
    }
  }

  bfs(0);

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>
#include <stdio.h>
using namespace std;
int n,m,l,p;
int a[100][100];
int D[100];
void bfs( int src) {
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while( ! Q.empty() ) {
		int cur = Q.front();
		Q.pop();
		for(int dst = 0; dst < n; dst++ ) {
			if( D[dst] < 0 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cout << i+1 <<  " " << D[i] <<  endl;
	}
}

int main() {
	cin >> n;
	for ( int i = 0;i < n;i++) {
		D[i] = -1;
		for(int j= 0; j < n;j++) {
			a[i][j] = 0;
		}
	}
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
	bfs(0);
}

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

int main() {
    int n;
    cin >> n;
    vector<vector<int>> graph(n, vector<int>(0));
    for (int i = 0; i < n; ++i) {
        int u, k;
        cin >> u >> k;
        --u;
        for (int j = 0; j < k; ++j) {
            int x;
            cin >> x;
            --x;
            graph[u].emplace_back(x);
        }
    }
    queue<int> q;
    vector<int> dist(n, -1);
    dist[0] = 0;
    q.push(0);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int nv : graph[v]) {
            if (dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            q.push(nv);
        }
    }
    for (int i = 1; i <= n; ++i) {
        cout << i << " " << dist[i - 1] << endl;
    }
}

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;  
}