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)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);

static const int N = 100;
static const int INF = (1<<21);

int d[N]; //距離で訪問状態を管理する
int n, M[N][N];

void bfs(int s){
  queue<int> q;
  q.push(s);

  REP(i,n) d[i] = INF;

  d[s] = 0;
  int u;
  while(!q.empty()){
    u = q.front(); q.pop();
    REP(v,n){
      //つながっていなかったら，飛ばす
      if(M[u][v] == 0) continue;
      //既に訪れていたら，飛ばす
      if(d[v] != INF) continue;

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

  REP(i,n){
    cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i] ) << endl;
  }




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

  REP(i,n){
    cin >> u >> k;
    --u;
    REP(i,k){
      cin >> v;
      --v;
      M[u][v] = 1;
    }
  }

  bfs(0);
  return 0;
}

C++: 
//push(),pop():C++ではstackと統一されいる
//front():先頭の要素が何かを返す
//back():末尾の要素が何であるかを返す
//empty():キューが空であるかどうかを調べる
//size():要素数を調べる
#include<stdio.h>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
static const int INFTY = (1<<21);

int G[MAX][MAX],color[MAX],d[MAX],num;

void bfs(int s){
  int u;
  queue<int> Q; //Qを使うことでエンキュー、デキューの操作が容易になる
  for(int i=1; i<=num; i++) {
    d[i] = INFTY;
  }
  Q.push(s);
  d[s] = 0;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();
    for(int v=1; v<=num; v++){
      if(G[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}


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

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

  bfs(1);

  for(i=1; i<=num; i++) {
    if(d[i] == INFTY) printf("%d -1\n",i);
    else printf("%d %d\n",i,d[i]);
  }

  return 0;
}

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

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n;
bool G[MAX + 1][MAX + 1];
int col[MAX + 1];
int d[MAX + 1];

void init(int n) {
	for (int i = 1; i <= n; i++) {
		col[i] = WHITE;
		d[i] = -1;
		for (int j = 1; j <= n; j++)
			G[i][j] = 0;
	}
}

void bfs(int u) {
	queue<int> Q;
	Q.push(u);
	col[u] = GRAY;
	d[u] = 0;
	while (!Q.empty()) {
		int v = Q.front();
		Q.pop();
		for (int i = 1; i <= n; i++) {
			if (G[v][i] && col[i] == WHITE) {
				Q.push(i);
				col[i] = GRAY;
				d[i] = d[v] + 1;
			}
		}
		col[v] = BLACK;
	}
}

int main() {
	cin >> n;
	init(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;
			G[u][v] = 1;
		}
	}
	bfs(1);
	for (int u = 1; u <= n; u++) {
		cout << u << " " << d[u] << endl;
	}
	return 0;
}

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


int main(){
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for(int i=0;i<n;++i){
    int id,k;
    cin >> id >> k;
    id--;
    for(int j=0;j<k;++j){
      int v;
      cin >> v;
      v--;
      g[id].push_back(v);
    }
  }
  vector<int> dist(n,-1);
  queue<int> que;
  dist[0]=0;
  que.push(0);
  while(!que.empty()){
    int v=que.front();
    que.pop();
    for(auto nv : g[v]){
      if(dist[nv]!=-1)continue;
      dist[nv]=dist[v]+1;
      que.push(nv);
    }
  }
  for(int i=0;i<n;++i){
    cout << i+1 << " " << dist[i] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <queue> 
using namespace std;
int G[100][100];
int d[100];
int color[100];
int n;
queue<int> qu;
void bfs ()
{
    color[0] = 1;
    d[0] = 0;
    qu.push(0);
    while (!qu.empty())
    { 
        int u = qu.front(); qu.pop();
        for (int i = 0; i < n; i++)
        {
            if (G[u][i] == 1 && color[i] == 0)
            {
                color[i] = 1;
                d[i] = d[u] + 1;
                qu.push(i);
            }
        }
        color[u] = 2;
    } 
    return;
}

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

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

    for (int i = 0; i < n; i++)
    {
        d[i] = -1; 
        color[i] = 0;
    } 
    bfs();
    for (int i = 0; i < n; i++)
    {
         cout<< i+1<<" "<<d[i]<<endl;
    } 
    return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(long long i=0;i<n;++i)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
using namespace std;
const int INF=1<<30;
int n,graph[101][101],v[101];
void bfs(int s){
    queue< int > q;
    fill(v,v+101,INF);
    q.push(s);
    v[s]=0;
    while(!q.empty()){
        int p=q.front();q.pop();
        FOR(i,1,n+1){
            if(!graph[p][i]) continue;
            if(v[i]==INF){
                v[i]=v[p]+1;
                q.push(i);
            }
        }
    }
    FOR(i,1,n+1){
        printf("%d ",i);
        printf("%d\n",v[i]!=INF?v[i]:-1);
    }
}
int main(){
    int u,k,e;
    scanf("%d",&n);
    REP(i,n){
        scanf("%d%d",&u,&k);
        REP(j,k){
            scanf("%d",&e);
            graph[u][e]=1;
        }
    }
    bfs(1);
    return 0;
}