Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n, vector<int>(n,0));
    rep(i,n){
        int u,k;
        cin >> u >> k;
        u--;
        rep(i,k){
            int v;
            cin >> v;
            v--;
            G[u][v] = 1;
        }
    }
    queue<int> q;
    vector<int> d(n,-1);
    d[0] = 0;
    q.push(0);
    while(!q.empty()){
        int p = q.front();
        q.pop();
        rep(i,n){
            if(G[p][i] == 1 && d[i] == -1){
                //cout << G[p][i] << " " << d[i] << endl;
                q.push(i);
                d[i] = d[p]+1;
                //cout << d[i] << endl;
            }
        }
    }
    rep(i,n){
        cout << i+1 << " " << d[i] << endl;
    }
    return 0;
}

C++: /**
 * Bredth first search.
 * graphs could be circular.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N] = {0};

void bfs(int n, int dd) {
	if (d[n] != -1) return;
	d[n] = dd++;
	int k = adj[n][0];
	for (int j=1;j<=k;j++) {
		bfs(adj[n][j], dd);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		d[j]=-1;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
	}

	bfs(1, 0);

	for (int i=1;i<=n;i++){
		cout << i << ' ' << d[i] << endl;
	}

	return 0;
}

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

#define NIL -1
#define INF 1000000001

using namespace std;
void bfs(const vector<vector<int> > &adj, vector<int> &d, queue<int> node_visit){
    int current_node;
    while(!node_visit.empty()){
        current_node = node_visit.front();
        node_visit.pop();
        for(int i = 1;i < d.size();i++){
            if(adj[current_node][i] == 1 && d[i] == -1){
                d[i] = d[current_node]+1;
                node_visit.push(i);
            }
        }
    }
    
}
int main(){
    int num_node;
    int node_ind,node_deg,adj_node;
    scanf("%d",&num_node);

    vector<vector<int> > adj(num_node+1,vector<int>(num_node+1,0));
    queue<int> node_visit;
    vector<int> d(num_node+1,NIL);

    for(int i = 0; i < num_node;i++ ){
        scanf("%d %d",&node_ind,&node_deg);
        for(int j = 0;j < node_deg;j++){
            scanf("%d",&adj_node);
            adj[node_ind][adj_node] = 1;
        }
    }
   
    node_visit.push(1);
    d[1] = 0;
    bfs(adj,d,node_visit);
    for(int i = 1; i < num_node+1;i++){
        printf("%d %d\n",i,d[i]);
    }

    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]; 
/* d[N] are distances which describe the state whether the node is already visited.
   if the value == INFTY, the edge is NOT yet visited.
 */

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--; // because the index is 0-origin!
        for (int j = 0; j < k; j++) {
            cin >> v;
            v--; // because the index is 0-origin!
            M[u][v] = 1; // edges between u and v are available (connected).
        }
    }
    
    bfs(0);
    
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
static const int MAX=101;

vector<int> Adjacency[MAX];
int dt[MAX];
bool Visited[MAX];

void bfs(int i)
{
    queue<int> Q;
    dt[i]=0;Q.push(i);
    Visited[i]=true;
    int d=0;
    while(!Q.empty())
    {
        int p=Q.front();Q.pop();d=dt[p]+1; //important
        for(int j=0;j<Adjacency[p].size();j++)
        {
            if(Visited[Adjacency[p][j]]==true) continue;
            //else
            Q.push(Adjacency[p][j]); Visited[Adjacency[p][j]]=true;
            dt[Adjacency[p][j]]=d;
        }
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int j,num;
        cin>>j>>num;
        for(int k=0;k<num;k++)
        {
            int t;cin>>t;
            Adjacency[j].push_back(t);
        }
    }
    bfs(1);
    for(int i=1;i<=n;i++)
    {
        cout<<i<<' '<<dt[i]<<endl;
    }
}