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>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
using namespace std;
int G[100][100];
queue<int> q;

class ANS
{
public:
	int id, d;
	bool done;
};

void bfs(ANS a[])
{
	q.push(a[0].id);
	a[q.front() - 1].done = true;
	a[q.front() - 1].d++;
	while (q.empty() != true)
	{
		int Q = q.front();
		FOR(i, 1, 101)
		{
			if (G[Q-1][i-1] == 1 && a[i-1].done == false)
			{
				a[i-1].done = true;
				a[i-1].d = a[Q-1].d + 1;
				q.push(i);
			}
		}
		q.pop();
	}
}

int main()
{
	ANS ans[100];
	int n;
	cin >> n;
	FOR(i, 0, n)
	{
		ans[i] = { i + 1,-1,false };
		int u, m;
		cin >> u >> m;
		FOR(j, 0, m)
		{
			int l;
			cin >> l;
			G[u - 1][l - 1] = 1;
		}
	}
	bfs(ans);
	FOR(i, 0, n)
	{
		cout << ans[i].id << " " << ans[i].d << endl;
	}
	return 0;
}

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

using namespace std;

int D[110]={0};
void bfs(int src,int n,int g[110][110]){

queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
	int cur =Q.front();
	Q.pop();
	for(int r=0;r<n;r++){
		if (g[cur][r]==1&&D[r]==0){
			D[r]=D[cur]+1;
			Q.push(r);
		}
	}
}
	
}



int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
	cin>>t;
	g[k][t-1]=1;
}
}
bfs(0,n,g);
cout<<"1 "<<D[0]<<endl;
for(int a=1;a<n;a++){
	if(D[a]!=0){
	cout<<a+1<<" "<<D[a]<<endl;
}
else{
	cout<<a+1<<" "<<-1<<endl;
}
}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int MAX = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int NIL = -1;
int M[MAX][MAX], n;
int d[MAX], color[MAX];
queue<int> Q;

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

int main()
{
    int id, m, v;
    cin >> n;
    for (int i = 0; i != MAX; ++i)
    {
        d[i] = NIL;
        color[i] = WHITE;
        for (int j = 0; j != MAX; ++j)
        {
            M[i][j] = 0;
        }
    }
    for (int i = 0; i != n; ++i)
    {
        cin >> id >> m;
        for (int j = 0; j != m; ++j)
        {
            cin >> v;
            M[id-1][v-1] = 1;
        }
    }
    bfs(0);

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

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

using namespace std;

#define N 100
#define INFTY 99999999

int n,
// 隣接行列
M[N][N];
// 距離 colorも同時に管理する
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] == 1 && d[v] == INFTY){
                // 対象の頂点の距離は前の頂点 + 1
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
    
}

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

    for(int i = 0;i < n;++i){
        cout << i + 1 << " " << ((d[i] == INFTY) ? (-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];

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[u] != 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 <iostream>
#include <queue>
using namespace std;

void bfs(int);

static const int Q = 100;
static const int W = (1<<21);

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

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

void bfs(int s){
  queue<int> q;
  q.push(s);

  for(int i=0;i<n;i++){
    d[i]=W;
  }
  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]!=W)continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }

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