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>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int a[101][101],ans=-1,check[101],n;

void calc(int now,int g,int count){
  if(count>ans && ans!=-1) return;
  if(now==g){
    if(ans==-1){
      ans=count;
      return;
    }
    else{
      ans=min(ans,count);
      return;
    }
  }
  if(check[now]>count) check[now]=count;
  else return;
  lp(i,n+1){
    if(a[now][i]==1)
      calc(i,g,count+1);
  }
}

int main(){
  cin>>n;
  lp(i,n){
    int x,y;
    cin>>x>>y;
    lp(j,y){
      int z;
      cin>>z;
      a[x][z]=1;
    }
  }
  lp(i,n){
    lp(j,101){
      check[j]=1000;
    }
    ans=-1;
    calc(1,i+1,0);
    cout<<i+1<<" "<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#define Lens 105
using namespace std;
int N;
int A[Lens][Lens];
int d[Lens];
char c[Lens];
queue<int> Q;

int main()
{
	cin >> N;
	Q.push(1);
	for (int i = 1; i <= N; i++)
	{
		int k, n;
		cin >> k >> n;
		c[k] = 'w';
		for (int i = 0; i < n; i++)
		{
			int j;
			cin >> j;
			A[k][j] = 1;
		}
	}
	while (Q.size())
	{
		int i = Q.front();
		Q.pop();
		for(int j=1;j<=N;j++)
		{
			if (A[i][j] == 1 && c[j] == 'w')
			{
				c[j] = 'g';
				d[j] = d[i] + 1;
				Q.push(j);
			}	 
		}
	}
	for (int i = 2; i <= N; i++)
	{
		if (c[i] == 'w')
			d[i] = -1;
	}	
	for (int i = 1; i <= N; i++)
	{
		cout << i << ' ' << d[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;

const int INF = 1 << 10;
const int MAX_N = 110;
const int DONE = 2;
const int TOUCHED = 1;
const int NOT_TOUCHED = 0;
int G[MAX_N][MAX_N];
int D[MAX_N];
int V[110];

int main() {
  int n, i, j, u, k, v, d;
  queue<int> q;

  cin >> n;
  for (i = 0; i < n; ++i) {
    D[i] = INF;
  }

  for (i = 0; i < n; ++i) {
    cin >> u >> k;
    for (j = 0; j < k; ++j) {
      cin >> v;
      G[u - 1][v - 1] = 1;
    }
  }

  D[0] = 0;
  V[0] = TOUCHED;
  q.push(0);
  while (!q.empty()) {
    i = q.front(); q.pop();
    for (j = 0; j < n; ++j) {
      if (G[i][j] && V[j] != DONE) {
        if (V[j] == NOT_TOUCHED) {
          V[j] = TOUCHED;
          q.push(j);
        }
        D[j] = min(D[j], D[i] + 1);
      }
    }
    V[i] = DONE;
  }

  for (i = 0; i < n; ++i) {
    d = D[i] == INF ? -1 : D[i];
    cout << i + 1 << ' ' << d << '\n';
  }

  return 0;
}

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

using i64 = int64_t;

using Graph = vector<vector<int>>;

int main(){
    int n;
    cin >> n;
    Graph graph(n+1, vector<int>());
    for(int i=0;i<n;++i){
        int u, k;
        cin >> u >> k;
        for(int j=0;j<k;++j){
            int v;
            cin >> v;
            graph[u].push_back(v);

        }
    }

    vector<int> mini(n+1, 200);
    queue<pair<int, int>> que;
    mini[1] = 0;
    que.push({1, 0});
    while(!que.empty()){
        auto cur = que.front(); que.pop();
        for(auto to: graph[cur.first]){
            int cost = cur.second + 1;
            if(mini[to] <= cost)continue;
            mini[to] = cost;
            que.push({to, cost});
        }
    }

    for(int i=1;i<=n;++i){
        cout << i << " " << (mini[i] < 200 ? mini[i] : -1) << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using Graph = vector<vector<int>>;
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
const int inf = 1e8;

int main() {
    int n;
    cin >> n;
    Graph g(n);
    rep(i,n) {
        int id,k;
        cin >> id >> k;
        id--;
        rep(j,k) {
            int v;
            cin >> v;
            v--;
            g[id].push_back(v);
        }
    }
    vector<int> d(n);
    rep(i,n) d[i] = inf;
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int v = q.front(); q.pop();
        for (int n_v: g[v]) if (chmin(d[n_v],d[v]+1)) q.push(n_v);       
    };
    rep(i,n) if (d[i] == inf) d[i] = -1;
    rep(i,n) cout << i+1 << " " << d[i] << endl;
}

C++: #include<cstdio>
#include<queue>
#define MAX 100
using namespace std;
int n, g[MAX][MAX], labels[MAX], d[MAX];
queue<int> Q;

void bfs(int s) {
    int v,u;
    labels[s] = 1;
    d[s]=0;
    Q.push(s);
    while (!Q.empty()) {
	u = Q.front(); Q.pop();
	for (v=0; v<n; v++) {
	    if (g[u][v] && labels[v]==0) {
		labels[v] =1;
		d[v] = d[u] +1;
		Q.push(v);
	    }
	}
	labels[u] = 2;
    }
}

int main() {
    int i,u,k,v,j;
    scanf("%d",&n);
    for(i=0;i<n;i++){
	for(j=0;j<n;j++){
	    g[i][j]=0;
	}
    }

    for(i=0;i<n;i++){
	scanf("%d %d",&u, &k);
	for(j=0;j<k;j++){
	    scanf("%d",&v);
	    g[u-1][v-1]=1;
	}
    }

    for(i=0; i<n; i++) { labels[i]=0; d[i]=0;}

    bfs(0);

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

    return 0;
}