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<bits/stdc++.h>
using namespace std;

typedef pair< int,int > P;
int N;
vector< vector< int > > Edge;
vector< int > Csum;

void bfs(){
  Csum.assign(N+1,-1);
  queue< P > que;
  que.push(P(0,0));
  Csum[0] = 0;
  while(!que.empty()){
    int now = que.front().second;
    int cost = que.front().first;
    que.pop();
    for(int i=0;i<Edge[now].size();i++){
      if(Csum[Edge[now][i]] != -1) continue;
      Csum[Edge[now][i]] = cost+1;
      que.push(P(cost+1,Edge[now][i]));
    }
  }
}

int main(){
  cin >> N;
  Edge.resize(N+1);
  for(int i=0;i<N;i++){
    int u,k; cin >> u >> k;
    for(int j=0;j<k;j++){
      int e; cin >> e; e--;
      Edge[i].push_back(e);
    }
  }
  bfs();
  for(int i=0;i<N;i++){
    cout << i+1 << " " << Csum[i] << endl;
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N=100;
static const int INFTY=(1<<21);

int M[N][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 k,u,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 <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stdio.h>
#define REP(i, n) for(int i = 0;i < n;i++)

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);
	REP(i, n)
		d[i] = INFTY;
	d[s] = 0;
	int u;

	while (!q.empty())
	{
		u = q.front();
		q.pop();
		REP(v, n)
		{
			if (M[u][v] == 0)
				continue;
			if (d[v] != INFTY)
				continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	REP(i, n)
		cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
}

int main()
{
	int u, k, v;

	cin >> n;

	REP(i, n)
	{
		REP(j, n)
			M[n][n] = 0;
	}

	REP(i, n)
	{
		cin >> u >> k;
		u--;
		REP(j, k)
		{
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}



	bfs(0);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N]; // 頂点0からの距離

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] && d[v] == INFTY) {
                // 未訪問
                q.push(v);
                d[v] = d[u] + 1;
            }
        }
    }

    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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define WHITE 0
#define GRAY 1
#define BLACK 2

static const int INFTY = (1<<21);
static const int N=100;

int n, M[N][N];
int color[N], d[N], f[N], tt;

// void dfs_visit(int u) {
//    color[u] = GRAY;
//    d[u] = ++tt;
//    rep(v, n) {
//       if (M[u][v] == 0) continue;

//       if(color[v] == WHITE) {
//          dfs_visit(v);
//       }
//    }
//    color[u] = BLACK;
//    f[u] = ++tt;
// }

// void dfs() {
//    int u;
//    for(u = 0; u < n; u ++) {
//       color[u] = WHITE;
//    }

//    for(u = 0; u < n; u ++) {
//       if(color[u] == WHITE) dfs_visit(u);
//    }

//    for(u =0; u<n;u++) {
//       printf("%d %d %d\n", u+1, d[u], f[u]);
//    }
// }

// void bfs_visit() {

// }

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

   rep(i, n) {
      d[i] = INFTY;
      color[i] = WHITE;
   }

   q.push(s);
   d[s] = 0;

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

   rep(i, n) {
      if(d[i] != INFTY) {
         cout << i+1 << " " << d[i] << endl;
      } else {
         cout << i+1 << " " << -1 << endl;
      }
   }
}

int main(){
   int u, k, v;
   cin>>n;

   // graphを0で初期化
   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 ++) {
      scanf("%d %d", &u, &k);
      u--;
      for(int j = 0; j < k; j++) {
         scanf("%d", &v);
         v--;
         M[u][v] = 1;
      }
   }

   bfs(0);
   // dfs();
   return 0;
}