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 <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <stack>

using namespace std;

#define mod 1000000007

int main()
{
	int n;
	int graph[101][101] = {};
	cin >> n;
	int ans[101] = {};
	for(int i = 0; i < n; i++){
		ans[i] = -1;
		int u, k;
		cin >> u >> k;
		u--;
		for(int j = 0; j < k; j++){
			int v;
			cin >> v;
			v--;
			graph[u][v] = 1;
		}
	}
	queue<int> qu;
	qu.push(0);
	ans[0] = 0;
	while(!qu.empty()){
		int tmp = qu.front();
		qu.pop();
		for(int i = 0; i < n; i++){
			if(ans[i] == -1 && graph[tmp][i] > 0){
				ans[i] = ans[tmp] + 1;
				qu.push(i);
			}
		}
	}
	for(int i = 0; i < n; i++){
		cout << i + 1 << " " << ans[i] << endl;
	}
}

C++: # -*- coding: utf-8 -*-
from collections import deque

N = int(input())
adj = [[0]*N for _ in range(N)]
unsearched = set(range(1, N))
result = {n:-1 for n in range(N)}

for n in range(N):
    inp = list(map(int, input().split()))
    for i in inp[2:]:
        adj[inp[0]-1][i-1] = 1

que = deque([[0, 0]]) # edge distance


while len(que):
    q = que.pop()
    result[q[0]] = q[1]
    next_ = [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)]
    unsearched = unsearched.difference([i[0] for i in next_])
    que.extendleft(next_)

for n in range(N):
    print("{} {}".format(n+1, result[n]))

C++: // J4180449 三宅亮輔
// 問題名
// - Breadth First Search
// 問題URL
// - https://onlinejudge.u-aizu.ac.jp/#/problems/ALDS1_11_C
// 所要時間
// -
// 学んだこと
//
//
//

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

typedef long long ll;

int n, d[100];

vector<int> v[100];

queue<int> q;

int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        d[i] = -1;
        int u, k;
        cin >> u >> k;
        u--;
        for(int j = 0; j < k; j++) {
            int x;
            cin >> x;
            x--;
            v[u].push_back(x);
        }
    }

    d[0] = 0;
    q.push(0);

    while(!q.empty()) {
        int vi = q.front();
        q.pop();

        for(int i = 0; i < v[vi].size(); i++) {
            int next = v[vi][i];
            if(d[next] == -1) {
                d[next] = d[vi] + 1;
                q.push(next);
            }
        }
    }

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

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

struct VRTX {
	int v[100] = {}, dist = -1;
};

int n, curdist = 0;
VRTX u[100];

void breadthFirstSearch(int x, int dist);

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

	breadthFirstSearch(1, 0);

	for (i = 1; i <= n; i++) {
		cout << i << " " << u[i].dist << endl;
	}

	return 0;
}

void breadthFirstSearch(int x, int dist) {
	int i, nextdist;

	if (u[x].dist == -1)u[x].dist = dist;
	else if (u[x].dist <= dist)return;
	else u[x].dist = dist;
	
	i = 0;
	nextdist = u[x].dist + 1;
	while (u[x].v[i] != 0) {
		breadthFirstSearch(u[x].v[i], nextdist);
		i++;
	}
}

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

const int nIMPOS = -1;
struct StDir
{
  int m_nBgn;
  int m_nEnd;

  StDir(int nBgn, int nEnd) : m_nBgn(nBgn), m_nEnd(nEnd)
  { }
};

void fnInput(queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  int nxSiz;
  cin >> nxSiz;

  rvnDst.resize(nxSiz + 1, nIMPOS);
  rvnDst[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;
      if (nx != ny)
      {
        StDir oDir(nx, ny);
        rqoBFSrch.push(oDir);
      }
    }
  }
}

void fnBreadthFirstSearch(queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  while (rqoBFSrch.size())
  {
    StDir oDir = rqoBFSrch.front(); rqoBFSrch.pop();

    if (rvnDst[oDir.m_nBgn] == nIMPOS)  continue;

    int nDst = rvnDst[oDir.m_nBgn] + 1;
    if (rvnDst[oDir.m_nEnd] == nIMPOS)
      rvnDst[oDir.m_nEnd] = nDst;
    else
      rvnDst[oDir.m_nEnd] = min(rvnDst[oDir.m_nEnd], nDst);
  }
}

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

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  queue<StDir> qoBFSrch;
  vector<int>  vnDst;

  fnInput(qoBFSrch, vnDst);
  fnBreadthFirstSearch(qoBFSrch, vnDst);
  fnResult(vnDst);

  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#define FOR(i,k,n) for(int (i) = (k); (i) < (n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x,y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
template<class T> using vv=vector< vector<T> >;
typedef deque<int> di;
typedef deque< deque<int> > ddi;


int main() {
    int n;
    cin >> n;
    vi vlist[101];
    rep(i,n) {
        int u,k;
        cin >> u >> k;
        rep(i,k) {
            int v;
            cin >> v;
            vlist[u].pb(v);
        }
    }

    queue<int> Q;
    int count[101];
    Q.push(1);

    while(!Q.empty()) {
        int i = Q.front();
        Q.pop();
        
        rep(j,vlist[i].size()) {
            if(count[vlist[i][j]] == 0) {
                count[vlist[i][j]] = count[i] + 1;
                Q.push(vlist[i][j]);
            }
        }
    }
    FOR(i,1,n+1) {
        if(i != 1 && count[i] == 0) {
            cout << i << " " << -1 << endl;
        } else {
            cout << i << " " << count[i] << endl;
        }
    }

	return 0;
}