Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <queue>

using namespace std;

const int WHITE = 0, GRAY = 1, BLACK = 2;

struct BFS {
    int n;
    vector<vector<int>> g;
    vector<int> depth;
    vector<int> state;
    int time = 0;

    BFS(int n) : n(n) {
        g.assign(n, vector<int>());
        depth.assign(n, -1);
        state.assign(n, WHITE);
    }

    void addEdge(int u, int v) {
        g[u].push_back(v);
    }

    void bfs(int s) {
        queue<int> q;
        state[s] = GRAY;
        depth[s] = 0;
        q.push(s);

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            if (state[u] == BLACK) {
                return;
            }
            for (int v : g[u]) {
                if (state[v] == WHITE) {
                    state[v] = GRAY;
                    depth[v] = depth[u] + 1;
                    q.push(v);
                }
            }
            state[u] = BLACK;
        }
    }

    void print() {
        for (int i = 0; i < n; i++) {
            cout << i << ":";
            for (int v : g[i]) {
                cout << v << " ";
            }
            cout << endl;
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    BFS dfs(n);
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            v--;
            dfs.addEdge(u, v);
        }
    }

    //dfs.print();

    dfs.bfs(0);

    for (int i = 0; i < n; i++) {
        cout << (i + 1) << " " << dfs.depth[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>

class Graph{
public:
	std::vector< std::vector<int> > A;
	std::vector<int> d;
	std::list<std::vector<int> > Q;
	int n;

	Graph(int n);

	void BreadthFirstSearch();

};

Graph::Graph(int n){
	A.resize(n);
	d.resize(n);
	for (int i = 0; i < n; i++){
		A[i].resize(n);
		for (int j = 0; j < n; j++){
			A[i][j] = 0;
		}
		d[i] = -1;
	}
	this->n = n;
}

void Graph::BreadthFirstSearch(){
	d[0] = 0;
	std::vector<int> a(2, 0);
	a[1] = 1;
	Q.push_back(a);
	while (!Q.empty()){
		std::vector<int> f = Q.front();
		Q.pop_front();
		for (int i = 0; i < n; i++){
			if (A[f[0]][i] == 1 && d[i] == -1){
				d[i] = f[1];
				a[0] = i;
				a[1] = f[1] + 1;
				Q.push_back(a);
			}
		}
	}
	return;
}

int main(){
	int n, u, k, v;
	scanf("%d", &n);
	Graph G(n);
	for (int i = 0; i < n; i++){
		scanf("%d%d", &u, &k);
		for (int j = 0; j < k; j++){
			scanf("%d", &v);
			G.A[u - 1][v - 1] = 1;
		}
	}

	G.BreadthFirstSearch();

	for (int i = 0; i < n; i++){
		printf("%d %d\n", i+1, G.d[i]);
	}


	return 0;
}

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

using namespace std;



int main() {
	queue<pair<int, int>> q;
	int n,row,size,tmp;
	pair<int, int> ptmp;
	cin >> n;
	int** mat = new int* [n];
	int *distance = new int[n];
	for (int i = 0;i < n;i++) {
		cin >> row >> size;
		mat[row - 1] = new int[n];
		for (int j = 0;j < n;j++) {
			mat[row - 1][j] = 0;
		}
		for (int j = 0;j < size;j++) {
			cin >> tmp;
			mat[row - 1][tmp - 1] = 1;
		}
		distance[i] = -1;
	}
	distance[0] = 0;
	q.push(make_pair(0, 0));
	while (!q.empty()) {
		ptmp = q.front();
		q.pop();
		for (int i = 0;i < n;i++) {
			if (mat[ptmp.first][i] && distance[i] == -1) {
				distance[i] = ptmp.second + 1;
				q.push(make_pair(i, ptmp.second + 1));
			}
		}
	}
	for (int i = 0;i < n;i++) {
		cout << i + 1 << " " << distance[i] << endl;
	}


}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
	int degree,number,num,count;
	cin>>degree;
	int arr[100][100];
	for(int i=0;i<degree;i++)
	{
		for(int j=0;j<degree;j++)
		{
			arr[i][j]=0;
		}
	}
	for(int i=0;i<degree;i++)
	{
		cin>>count;
		cin>>number;
		for(int j=0;j<number;j++)
		{
			cin>>num;
			arr[i][num-1]=1;
		}
	}
	int queue[100];
	int L=0,R=0;
	int ans[degree];
	bool flag=false;
	for(int i=0;i<degree;i++)
	{
		ans[i]=0;
	}
	R+=1;
	while(L!=R)
	{
		for(int i=0;i<degree;i++)
		{
			if(arr[queue[L]][i]!=0 && ans[i]==0)
			{
				queue[R++]=i;
				ans[i]=ans[queue[L]]+1;
			}
		}
		L++;
	}
	for(int i=0;i<degree;i++)
	{
		cout<<i+1<<" ";
		if(ans[i]==0 && i!=0)
		{
			cout<<"-1"<<endl;
		}
		else if(i!=0)
		{
			cout<<ans[i]<<endl;
		}
		if(i==0)
			cout<<0<<endl;
	}
}

C++: #include<iostream>
#include<queue>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
using namespace std;
int G[100][100];
queue<int> q;

class ANS
{
public:
	int id, d;
	bool done;
};

void bfs(ANS a[])
{
	q.push(a[0].id);
	a[q.front() - 1].done = true;
	a[q.front() - 1].d++;
	while (q.empty() != true)
	{
		int Q = q.front();
		FOR(i, 1, 100)
		{
			if (G[Q-1][i-1] == 1 && a[i-1].done == false)
			{
				a[i-1].done = true;
				a[i-1].d = a[Q-1].d + 1;
				q.push(i);
			}
		}
		q.pop();
	}
}

int main()
{
	ANS ans[100];
	int n;
	cin >> n;
	FOR(i, 0, n)
	{
		ans[i] = { i + 1,-1,false };
		int u, m;
		cin >> u >> m;
		FOR(j, 0, m)
		{
			int l;
			cin >> l;
			G[u - 1][l - 1] = 1;
		}
	}
	bfs(ans);
	FOR(i, 0, n)
	{
		cout << ans[i].id << " " << ans[i].d << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;

void bfs(int start, int n, vector<vector<int>> &a) {
	int INF = 10000000;
	vector<int> d(n, INF);
	queue<int> q;
	d[start] = 0;
	q.push(start);
	while (q.size() != 0) {
		int u = q.front();
		q.pop();
		for (int i = 0; i < n; i++) {
			if (a[u][i] == 0)
				continue;
			if (d[i] != INF)
				continue;
			d[i] = d[u] + 1;
			q.push(i);
		}
	}
	for (int i = 0; i < n; i++) {
		if (d[i] == INF) {
			cout << i + 1 << " -1" << endl;
		}
		else {
			cout << i + 1 << " " << d[i] << endl;
		}
	}
}
int main()
{
	int n;
	vector<vector<int>> a;
	cin >> n;
	a.resize(n);
	for (int i = 0; i < n; i++) {
		a[i].resize(n);
	}
	for (int i = 0; i < n; i++) {
		int temp, k;
		cin >> temp >> k;
		for (int j = 0; j < k; j++) {
			int temp2;
			cin >> temp2;
			a[temp - 1][temp2 - 1] = 1;
		}
	}

	bfs(0, n, a);

	return 0;
}