Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21); // ?????????????????????(2^21)

int n,M[N][N];
int d[N]; // ?????¢??§?¨??????¶???(color)???????????????

void bfs(int s){
	queue<int> q; // ?¨??????????????????????queue????????¨
	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(); // ?????\???????????????u???????????????
		for(int v=0;v<n;v++){
			if(M[u][v] == 0) continue; // ??????u????????????v??????????????????????????°??????
			if(d[v] != INFTY) continue; // ??????v????????§????¨???????????????¨???????????°??????
			d[v] = d[u] + 1; // ??????v????????????u?????°?????????+1?????°??????????????\?????????
			q.push(v); // ?????\????????????v?????\??????
		}
	}
	for(int i=0;i<n;i++){
		cout << i+1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl; // ??????i?????°????????????INFTY??????-1???????????\???????????°????????????????????????
	}
}

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

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
static const int NAV = -1;

int M[N][N];

void bfs(int s, int n, int M[N][N], int d[]) {
    queue<int> Q;
    for (int i = 0; i < n; i++) d[i] = NAV;
    d[s] = 0;
    Q.push(s);

    int u;
    while (!Q.empty()) {
        u = Q.front();
        Q.pop();
        for (int v = 0; v < n; v++) {
            if (M[u][v] != 0 && d[v] == NAV) {
                d[v] = d[u] + 1;
                Q.push(v);
            }
        }
    }
}

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

    int d[n];
    bfs(0, n, M, d);

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

    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]; // ?????¢??§?¨??????¶???(color)???????????????

void bfs(int s) {
  queue<int> q; // ?¨?????????????????????? queue ????????¨
  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, i, j;

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

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

  bfs(0);

  return 0;
}

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

queue<int>Q;
int n, M[101][101], s[101];

void bfs()
{
	Q.push(1);
	s[1] = 0;

	int id = 1;

	while (true)
	{
		for (int pid = 1; pid <= n; pid++)
		{
			if (M[id][pid] == 1 && s[pid] == -1)
			{
				Q.push(pid);
				s[pid] = s[id] + 1;
			}
		}

		Q.pop();

		if (!Q.empty())
			id = Q.front();
		else
			break;
	}
}

int main()
{
	int id, pid, k;
	memset(M, 0, sizeof(M));
	memset(s, -1, sizeof(s));

	cin >> n;

	for (int i = 1; i <= n; i++)
	{
		cin >> id >> k;

		for (int j = 1; j <= k; j++)
		{
			cin >> pid;
			M[id][pid] = 1;
		}
	}

	bfs();

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

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main()
{
    int n;
    cin >> n;
    vector<vector<bool>> G(n, vector<bool>(n));
    rep(i, n)
    {
        int u, k;
        cin >> u >> k;
        rep(j, k)
        {
            int v;
            cin >> v;
            G[u - 1][v - 1] = true;
        }
    }
    queue<int> Q;
    Q.push(0);
    vector<bool> visited(n);
    vector<int> dis(n, -1);
    visited[0] = true;
    dis[0] = 0;
    while (!Q.empty())
    {
        int v = Q.front();
        Q.pop();
        rep(i, n)
        {
            if (G[v][i] && !visited[i])
            {
                Q.push(i);
                dis[i] = dis[v] + 1;
                visited[i] = true;
            }
        }
    }
    rep(i, n) printf("%d %d\n", i + 1, dis[i]);
    return 0;
}

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

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

  StDist(int nDist) : m_nDist(nDist), m_nFromMeTo(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();
    if (rvoDist[nx].m_nDist == nIMPOS)  continue;

    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
    {
      if (cnrvvnAdjcy[nx][ny] == 1  &&
          rvoDist[nx].m_nFromMeTo == 0)
      {
        int nDist = rvoDist[nx].m_nDist + 1;
        if (rvoDist[ny].m_nDist == nIMPOS)
          rvoDist[ny].m_nDist = nDist;
        else
          rvoDist[ny].m_nDist = min(rvoDist[ny].m_nDist, nDist);

        rqnBFSrch.push(ny);
      }
    }
    rvoDist[nx].m_nFromMeTo = 1;
  }
}

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