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 <stdio.h>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;
 
int main(void) {
  int n, ans = 0, inf = 10000;
  scanf("%d", &n);
  int dis[n];
  rep(i, n) dis[i] = inf;
  dis[0] = 0;
  vector< int > vec[n];
  rep(i, n) {
    int u, k;
    scanf("%d%d", &u, &k);
    u--;
    rep(j, k) {
      int v;
      scanf("%d", &v);
      v--;
      vec[u].push_back(v);
    }
  }
  rep(i, n - 1) {
    rep(j, n) {
      vector< int >::iterator it;
      for(it = vec[j].begin(); it != vec[j].end(); ++it) {
        if(dis[*it] > dis[j] + 1) dis[*it] = dis[j] + 1;
      }
    }
    /*rep(j, n) printf("%d ", dis[j]);
    printf("\n");*/
  }
  rep(i, n) {
    if(dis[i] != inf) printf("%d %d\n", i + 1, dis[i]);
    else printf("%d -1\n", i + 1);
  }
  return 0;
}

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

using namespace std;

const int MAX = 100;
int adj[MAX][MAX] = {};

class BFS{
    int num, d[MAX], status[MAX];
public:
    BFS(int n) : num(n){
        for(int i=0;i<n;i++){
            d[i] = -1;
            status[i] = 0;
        }
    }

    void run(int node){
        queue<int> Q;
        Q.push(node);
        d[node] = 0;
        status[node] = 1;
        while(!Q.empty()){
            node = Q.front(); Q.pop();
            for(int i=0;i<num;i++){
                if(adj[node][i] && status[i] == 0) {
                    Q.push(i);
                    d[i] = d[node] + 1;
                    status[i] = 1;
                }
            }
        }
    }

    void print(){
        for(int i=0;i<num;i++){
            cout << i+1 << " " << d[i] << endl;
        }
    }
};

int main(){
    int r, c, n_c, n;
    cin >> n;
    BFS bfs(n);

    for(int i=0;i<n;i++){
        cin >> r >> n_c;
        for(int j=0;j<n_c;j++){
            cin >> c;
            adj[r-1][c-1]++;
        }
    }

    bfs.run(0);
    bfs.print();
}

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

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nDmy;

  StDist(int nDist) : m_nDist(nDist), m_nDmy(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
    {
      if (!cnrvvnAdjcy[nx][ny])          continue;
      if (rvoDist[ny].m_nDist != nIMPOS) continue; 

      int nDist = rvoDist[nx].m_nDist + 1;
      rvoDist[ny].m_nDist = nDist;
      rqnBFSrch.push(ny);
    }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}

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

const int INF = -1;

int G[110][110] = {};
int d[110] = {};

void bfs(int s, int n)
{
  int v;
  queue<int> que;

  for(int i = 1;i <= n;i++) d[i] = INF; //init

  que.push(s);
  d[s] = 0;
  
  while(que.size()){
    v = que.front();
    que.pop();

    for(int i = 1;i <= n;i++){
      if(G[v][i] != 1) continue;
      if(d[i] != INF) continue;

      que.push(i);
      d[i] = d[v]+1;
    }
  }
}

int main()
{
  int n;
  int i,j;
  int u,k,v;

  scanf("%d", &n);

  for(i = 0;i < n;i++){
    scanf("%d%d", &u, &k);

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

  for(i = 1;i <= n;i++)
    if(d[i] == 0) bfs(i, n);

  for(i = 1;i <= n;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];

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

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

vector<int> graph[101];
int n;

void bfs(int start){
	vector<int> d(n+1,-1);
	d[1]=0;

	queue<int> Q;
	Q.push(start);

	while(Q.size()){
		int u=Q.front();	Q.pop();
		for(int v:graph[u]){
			if(d[v]>=0)continue;

			d[v]=d[u]+1;
			Q.push(v);
		}
	}

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

	return;
}

int main(){
	cin>>n;
	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);
		}
	}


	bfs(1);

	return 0;
}

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

using namespace std;

int D[110]={0};
void bfs(int src,int n,int g[110][110]){

queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
	int cur =Q.front();
	Q.pop();
	for(int r=0;r<n;r++){
		if (g[cur][r]==1&&D[r]==0){
			D[r]=D[cur]+1;
			Q.push(r);
		}
	}
}
	
}



int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
	cin>>t;
	g[k][t-1]=1;
}
}
bfs(0,n,g);
cout<<"1 "<<D[0]<<endl;
for(int a=1;a<n;a++){
	if(D[a]!=0){
	cout<<a+1<<" "<<0<<endl;
}
else{
	cout<<a+1<<" "<<-1<<endl;
}
}
	return 0;
}