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<iostream>
#include<queue>
using namespace std;
int N;
int D[100];
void bfs(int src){
    int u[100],k[100],a[100],d[100][100];
    bool visit[100];
    for(int i=1;i<=N;++i){
        cin>>u[i]>>k[i];
        for(int j=0;j<k[i];++j){
            cin>>a[j];
            d[u[i]][a[j]]=1;
        }
        visit[i]=true;
    }
    queue<int>Q;
    Q.push(src);
    D[src]=0;
    visit[src]=false;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=1;i<=N;++i){
            if(d[cur][i]==1&&visit[i]){
                visit[cur]=false;
                visit[i]=false;
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    for(int i=1;i<=N;++i){
        if(i!=1&&D[i]==0){
            D[i]=-1;
        }
        cout<<i<<" "<<D[i]<<endl;
    }
}
int main(){
    cin>>N;
    int start=1;
    bfs(start);
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int M[N][N];
int d[N], n;

void bfs(int s){
  int i, u;
  queue<int> q;
  q.push(s);
  for(i=0;i<n;i++) d[i] = INFTY;
  d[s] = 0;
  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);
    }
  } // end while

  for(i=0;i<n;i++)cout << i+1 << " " << ((d[i] == INFTY)?(-1):d[i]) << endl;
  
}

int main(){
  
  int u, k, v;
  int i, j;
  
  cin >> n;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++)M[i][j] = 0;
  }

  for(i=0;i<n;i++){
    cin >> u >> k;
    u--; // change to origin
    for(j=0;j<k;j++){
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs(0);

  return 0;
}

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

int main(void)
{
     //??£??\??????????????£??\???????????????
    int n, k;

    cin >> n;
    vector<vector<bool> > A(n, vector<bool>(n, false));
    for(int i = 0; i < n; i++){
        cin >> k >> k;
        for(int j = 0; j < k; j++){
            int input;
            cin >> input;
            A[i][input-1] = true;
        }
    }

    vector<int> u(n, 1000000);
    u[0] = 0;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(u[j] == i){
                for(int k = 0; k < n; k++){
                    if(A[j][k] == 1) u[k] = min(i+1, u[k]);
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        if(u[i] != 1000000) cout << i + 1 << " " << u[i] << endl;
        else cout << i + 1 << " " << "-1" << endl;
    }
    return 0;

}