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>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
using namespace std;

static const int N = 100;
static const int INFTY = (1<<21); //十分に大きな数

int M[N][N];
int n;
int d[N]; //距離で訪問状態　color を管理する

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


  for(int i = 0; i < n; i++) d[i] = INFTY;
  d[s] = 0;

  //sに隣接の値をキューに入れる
  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;

//グラフの初期化
  REP(i,n){
    REP(j,n){
      M[i][j] = 0;
    }
  }

  //行列の作成
  REP(i,n){
    cin >> u >> k;
    u--;
    REP(j,k){
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs(0);

  return 0;

}

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

using namespace std;

const int INF = 100000000;

int main(){
	int n;
	cin >> n;
	vector<vector<int>> node(n+1);
	for(int i=0;i<n;i++){
		int u,k,tmp;
		cin >> u >> k;
		for(int j=0;j<k;j++){
			cin >> tmp;
			node[u].push_back(tmp);
		}
	}
	
	//search
	vector<int> res(n+1,INF);
	queue<int> que;
	queue<int> copied;
	queue<int> zeros;
	que.push(1);
	
	int count = 0;
	for(int i=0;i<n;i++){
		while(que.size()){
			int k = que.front(); que.pop();
			//cout << "-> " << k << endl;
			if(res[k] >= INF) res[k] = count;
			for(int v:node[k]){
				if(res[v] >= INF) copied.push(v);
			}
		}
		count++;
		que = queue<int>(copied);
		copied = queue<int>(zeros);
	}

	//output
	for(int i=1;i<=n;i++){
		if(res[i] >= INF) res[i] = -1;
		cout << i << " " << res[i] << endl;
	}

	return 0;
}

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

const int MAX = 100;
int n;
int G[MAX + 1][MAX + 1];
int states[MAX + 1];
int d[MAX + 1];
queue<int> Q;

void bfs(int id) {
  d[id] = 0;
  states[id] = 1;
  Q.push(id);

  while (!Q.empty()) {
    id = Q.front(); Q.pop();
    for (int i = 0; i < n; i++) {
      if (G[id][i] == 1 && states[i] == 0) {
        Q.push(i);
        states[i] = 1;
        d[i] = d[id] + 1;
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  int u, v, k;

  for (int i = 0; i < n; i++) {
    d[i] = -1;
    states[i] = 0;
    for (int j = 0; j < n; j++) {
      G[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    if (k > 0) {
      for (int j = 0; j < k; j++) {
        cin >> v;
        G[u - 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;
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;

  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;
const int MAX_N = 100;

int A[MAX_N+1][MAX_N+1];
int D[MAX_N+1];
int N;

void solve() {
  int d;
  queue<int> Q;

  Q.push(1);
  D[1] = d = 0;

  while (!Q.empty()) {
    int c = Q.front(); Q.pop();

    for (int i = 1; i <= N; i++) {
      if (A[c][i] == 1 && D[i] == -1) {
        D[i] = D[c]+1;
        Q.push(i);
      }
    }
  }
}

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      A[i][j] = 0;
    }
    D[i] = -1;
  }

  for (int i = 0; i < N; i++) {
    int u, k; cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int t; cin >> t;
      A[u][t] = 1;
    }
  }

  solve();

  for (int i = 1; i <= N; i++) {
    cout << i << ' ' << D[i] << endl;
  }

  return 0;
}

C++: # include <iostream>
# include <vector>
# include <cstdio>
# include <queue>
using namespace std;
struct info
{
	int d = -1;
	bool vis = false;
};
vector<int> a[105];
info *ans;
queue<int> solve;
void bfs()
{
	solve.push(1); 
	ans[1].vis = true; ans[1].d = 0;
	while(!solve.empty())
	{
		int u = solve.front(); solve.pop();
		for(int j = 0;j < a[u].size();j++)
		{
			if(!ans[a[u][j]].vis)
			{
				ans[a[u][j]].vis = true;
				ans[a[u][j]].d = ans[u].d + 1;
				solve.push(a[u][j]);
			}
		}
	}
	
}
int main(void)
{
	int n,cnt,k,u;
	scanf("%d",&n);
	ans = new info [n+1];
	for(int w = 0;w < n;w++)
	{
		scanf("%d %d",&u,&cnt);
		for(int i = 0;i < cnt;i++)
		{
			scanf("%d",&k);
			a[u].push_back(k);
		}
	}
	bfs();
	for(int i = 1;i <= n;i++) printf("%d %d\n",i,ans[i].d);
	return 0;
}