Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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;
}