Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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