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>
#include <vector>
#include <string>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <unordered_map>
#include <utility>
#include <map>
#include <set>
using namespace std;

int main(){
    int n;
    cin >> n;
    int g[110][110];
    int i, j;
    int u, k, v;
    int dist[110];
    bool visited[110];
    
    for(i = 0; i < n; i++){
        dist[i] = -1;
        for(j = 0; j < n; j++){
            g[i][j] = 0;
        }
    }
    
    for(i = 0; i < n; i++){
        cin >> u >> k;
        u--;
        for(j = 0; j < k; j++){
            cin >> v;
            v--;
            g[u][v] = 1;
        }
    }
    
    queue<int> q1, q2;
    q1.push(0);
    
    for(i = 0; q1.size(); i++){
        while(!q1.empty()){
            int p = q1.front();
            q1.pop();
            
            if(visited[p]) continue;
            visited[p] = true;
            
            dist[p] = i;
            
            for(j = 0; j < n; j++){
                if(g[p][j]){
                    q2.push(j);
                }
            }
        }
        q1 = q2;
        while(!q2.empty()){
            q2.pop();
        }
    }
    
    for(i = 0; i < n; i++){
        printf("%d %d\n", i+1, dist[i]);
    }
}

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

using namespace std;

template <class T>
struct Edge {
    int to;
    T cost;
    Edge(int to, T cost) : to(to), cost(cost){};
};

template <class T = int>
class Graph {
public:
    explicit Graph(int N = 0) : size(N) { path.resize(size); }
    void span(int u, int v, T cost = 1) { path[u].push_back(Edge<T>(v, cost)); }
    vector<Edge<T>> operator[](int v) const { return path[v]; }

    int size;
    vector<vector<Edge<T>>> path;
};

template <class T>
vector<T> bfs(const Graph<>& graph, int s) {
    vector<int> dist(graph.size, -1);
    dist[s] = 0;
    queue<int> que;
    que.push(s);

    while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (auto e : graph[v]) {
            if (dist[e.to] < 0) {
                dist[e.to] = dist[v] + 1;
                que.push(e.to);
            }
        }
    }
    return dist;
}

int main() {
    int N;
    cin >> N;
    Graph<> graph(N);
    for (int i = 0; i < N; ++i) {
        int v, k;
        cin >> v >> k;
        --v;
        for (int j = 0; j < k; ++j) {
            int sv;
            cin >> sv;
            graph.span(v, --sv);
        }
    }

    auto dist = bfs<int>(graph, 0);
    for (int v = 0; v < N; ++v) {
        cout << v + 1 << " " << dist[v] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;

using namespace std;

static const int N=100;
static const int INF =(1<<21);
int n,M[N][N];
int d[N];


void dfs(int s){
    queue<int> q;
    q.push(s);
    rep(i,n)d[i]=INF;
    d[s]=0;
    int u;
    while(!q.empty()){
        u=q.front();q.pop();
        for(int i=0;i<n;i++){
            if(M[u][i]==0)continue;
            if(d[i]!=INF)continue;
            d[i]=d[u]+1;
            q.push(i);
        }
    }
    rep(i,n){
        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++){
            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;
        }
    }
    
    dfs(0);
    
    
    

return 0;
}

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

using namespace std;

using Weight=int;
using DArray=vector<Weight>;
using DMatrix=vector<DArray>;

static const Weight INF=1<<29;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

int main() {
    size_t n;
    scanf("%zu", &n);

    Graph g(n);
    for (size_t i=0; i<n; ++i) {
        size_t k;
        scanf("%*d %zu", &k);
        for (size_t j=0; j<k; ++j) {
            size_t v;
            scanf("%zu", &v);
            --v;

            connect(g, i, v);
        }
    }

    queue<size_t> q; q.push(0);
    vector<size_t> d(n, -1); d[0]=0;
    while (!q.empty()) {
        size_t v=q.front(); q.pop();
        for (Edge &e: g[v]) {
            if (d[e.dst] > d[v] + 1) {
                d[e.dst] = d[v] + 1;
                q.push(e.dst);
            }
        }
    }

    for (size_t i=0; i<n; ++i)
        printf("%zu %zd\n", i+1, d[i]);

    return 0;
}

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

using namespace std;

static const int MAX = 100;
int n, m[MAX][MAX], vst[MAX];
int d[MAX];

struct ddat
{
	int nm;
	int d;
};


int next(int u) {

	for (int v = 0;v < n;v++) {
		if (m[u][v]) {
			return v;
		}
	}

	return -1;
}


void bfs() {

	queue<ddat> que;
	ddat  d1; d1.nm = 0;;d1.d = 0;
	que.push(d1);



	while (!que.empty())
	{
		ddat d2 = que.front();
		que.pop();
		vst[d2.nm] = 1;
		d[d2.nm] = min(d2.d, d[d2.nm]);

		for (int i = 0; i < n; i++)
		{
			if (m[d2.nm][i] != 0 && vst[i] == 0) {
				ddat dch;
				dch.nm = i;dch.d = d2.d + 1;
				
				que.push(dch);
			}
		}

	}

	return;
}


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;
		for (int j = 0;j < k;j++) {
			cin >> v;
			m[u - 1][v - 1] = 1;
		}
	}

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

	bfs();

	for (int i = 0; i < n; i++) {
		if (d[i] == 999999) d[i] = -1;

		cout << i + 1 << " " << d[i] << endl;
	}


	return 0;
}