Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

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

using namespace std;
static const int N = 100;
static const int INFTY = -1;

int n, M[N][N];
int d[N];

void bfs(int s)
{
	queue<int> q;
	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;
	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;
		}
	}
	bfs(0);
	return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
static const int MAX = 100;
int N;
int ADJ[MAX][MAX];
int depth[MAX] = {0};

enum STATUS { ACTIVE = 0, MARKED, VISITED, };
int status[MAX] = {ACTIVE};

void visit(int id) {
  status[id] = VISITED;
}
void mark(int id) {
  status[id] = MARKED;
}

void bfs(int id) {
  queue<int> q;
  q.push(id);
  depth[id] = 0;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    visit(u);
    for (int i = 0; i < N; i++) { 
      if (ADJ[u][i] && status[i] == ACTIVE) {
        q.push(i);
        mark(i);
        depth[i] = depth[u] + 1;
      }
    }
  }
}

int main() {
  cin >> N;
  int id, nn, x;
  for (int i = 0; i < N; i++) {
    cin >> id >> nn;
    id--; // 0-based
    for (int j = 0; j < nn; j++) {
      cin >> x;
      x--; // 0-based
      ADJ[id][x] = 1;
    }
  }

  bfs(0);
  for (int i = 0; i < N; i++)
    cout << i << " " << depth[i] << endl;
}

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

#define vv vector< vector< int > >
#define ll long long


int main(){
    int n;
    cin >> n;
    vv tree(n);
    for(int i = 0; i < n; ++i){
        int a, k;
        cin >> a >> k;
        a--;
        for(int j = 0; j < k; ++j){
            int b;
            cin >> b;
            b--;
            tree[a].push_back(b);
        }
    }
    vector<int> dist(n, -1);
    queue<int> que;
    dist[0] = 0;
    que.push(0);

    while(!que.empty()){
        int v = que.front();
        que.pop();

        for (int nv : tree[v]){
            if(dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    for(int v = 0; v < n; v++) cout << v+1 << " " << dist[v] << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
vector<int> graph[105];
bool vis[105];
int lvl[105];
int i;
void bfs(int rt)
{
    int src,adj;
    src=rt;
    deque<int>dq;
    vis[src]=1;
    lvl[src]=1;
    dq.push_back(src);
    while(!dq.empty())
    {
        src=dq.front();
        dq.pop_front();
        for(i=0; i<graph[src].size(); i++)
        {
            adj=graph[src][i];
            if(vis[adj]==0)
            {
                vis[adj]=1;
                lvl[adj]=lvl[src]+1;
                dq.push_back(adj);
            }
        }
    }
}
int main()
{
    int nodes,x,v;
    cin >> nodes;
    int u[200];
    memset(vis,0,sizeof(vis));
    memset(lvl,0,sizeof(lvl));
    for(i=1;i<=nodes;i++)
    {
        cin >> u[i];
        cin >> x;
        for(int j=0;j<x;j++)
        {
            cin >> v;
            graph[u[i]].push_back(v);
        }
    }
    bfs(1);
    for(i=1;i<=nodes;i++)
    {
        cout << u[i] << " " << lvl[u[i]]-1 << endl;
    }
}

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

using namespace std;

enum COLOR {
  WHITE,
  GRAY,
  BLACK,
};

static const int INFTY = INT_MAX;
static const int N = 100;

static int M[N][N] = {};
static vector<COLOR> color(N, WHITE);
static vector<int> d(N, INFTY);

static void bfs(int n, int s) {
  color[s] = GRAY;
  d[s] = 0;

  queue<int> Q;
  Q.push(s);

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

int main() {
  int n;
  cin >> n;

  int number, count, value;
  for (int i = 0; i < n; i++) {
    cin >> number >> count;
    number--;
    for (int j = 0; j < count; j++) {
     cin >> value;
     value--;
     M[number][value] = 1; 
    }
  }
  
  bfs(n, 0);
  
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? -1 : d[i]) << endl; 
  }

  return 0;
}

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 u[x].dist = min(u[x].dist, dist);
	
	i = 0;
	nextdist = u[x].dist + 1;
	while (u[x].v[i] != 0) {
		breadthFirstSearch(u[x].v[i], nextdist);
		i++;
	}
}