Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

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

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

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cstdio>
#include<queue>

using namespace std;

int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離

void bfs(int src){
	queue<int> Q;
	Q.push(src);
	fill(D, D + N, -1);
	D[src] = 0;
	while (!Q.empty()){
		int cur = Q.front();
		Q.pop();
		for (int k = 0; k < N; k++){
			if (G[cur][k] == 1 && D[k] == -1){
				D[k] = D[cur] + 1;
				Q.push(k);
			}
		}
	}
	for (int l = 0; l < N; l++){
		cerr << l + 1 << " " << D[l] << endl;
	}
}

int main(){
	scanf_s("%d", &N);
	int id, c;
	for (int i = 0; i < N; i++){
		scanf_s("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
		scanf_s("%d", V + id);
		for (int j = 0; j < V[id]; j++){
			scanf_s("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
			G[id][c] = 1;
		}
	}//データの読み込み完了。
	bfs(0);
}

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

int n;
vector<int> G[100];
int d[100];

int main() {
	int i;
	cin >> n;
	for ( i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for( i = 0; i < k; i++) cin >> v, G[u - 1].push_back(v - 1);
	}
	for (i = 0; i < 100; i++) d[i] = -1;

	queue<int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int v = q.front(); q.pop();
		for (int i = 0; i < G[v].size(); i++) {
			int x = G[v][i];
			if (d[x] == -1) {
				d[x] = d[v] + 1;
				q.push(x);
			}
		}
	}
	for (i = 0; i < n; i++) cout << i + 1 << " " << d[i] << endl;

}

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

using namespace std;

const int N = 100;
int n;
int M[N][N], color[N], d[N];

int main(){
  queue<int> q;
  cin >> n;
  int u, k, v;
  for(int i=0;i<n;i++){
    cin >> u >> k;
    for(int j=0;j<k;j++){
      cin >> v;
      M[u-1][v-1] = 1;
    }
  }
  for(int i=0;i<n;i++) d[i] = -1;
  d[0] = 0;
  color[0] = 1;
  q.push(0);
  int x;
  while(q.empty() == false){
    x = q.front();
    q.pop();
    for(int i=0;i<n;i++){
      if(M[x][i] == 0) continue;
      if(color[i] == 0){
        color[i] = 1;
        d[i] = d[x] + 1;
        q.push(i);
      }
    }
  }
  for(int i=0;i<n;i++){
    cout << i+1 << " " << d[i] << endl;
  }
  return 0;
}

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

using namespace std;

int n;
int vertex[101];
int graph[101][101];
int dis[101];

queue<int> Q;

int main() {
	cin >> n;
	int k,d;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			graph[i][j] = -1;
		}
		dis[i] = -1;
	}


	for (int i = 1; i <= n; ++i) {
		cin >> vertex[i] >> k;
		for (int j = 0; j < k; ++j) {
			cin >> d;
			graph[i][d] = 1;
		}
	}

	Q.push(1);
	dis[1] = 0;

	while (!Q.empty()) {
		k = Q.front();
		Q.pop();
		for (int i = 1; i <= n; ++i) {
			if (graph[k][i] == 1 && dis[i] == -1) {
				Q.push(i);
				dis[i] = dis[k]+1;
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		cout << i << ' ' << dis[i] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
vi M(MAX, -1);
int main() {
	int n; cin >> n;
	M[1] = 0;
	deque<pair<int, int>> dq;
	int u, k, v;
	rep(i, 0, n) {
		cin >> u >> k;
		rep(j, 0, k) {
			cin >> v;
			if(u == 1) M[v] = 1;
			else dq.eb(mp(u, v));
		}
	}
	while(!dq.empty()) {
		tie(u, v) = dq.front(); dq.pop_front();
		if(M[u] == -1) dq.eb(mp(u, v));
		else if(M[v] == -1 || M[v] > M[u] + 1) M[v] = M[u] + 1;
	}
	rep(i, 1, n + 1) {
		cout << i << " " << M[i] << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<queue>
#define MAX 100
#define INFTY -1

using namespace std;
queue<int> Q;
int d[MAX];
int A[MAX][MAX]={0};
int n;

void bfs(int u)
{
	Q.push(u);
	d[u] = 0;

	while(!Q.empty())
	{
		int v = Q.front();
		Q.pop();
		for(int i=0; i<n; i++)
		{
			if(A[v][i] == 1)
			{
				if(d[i] == INFTY)
				{
					Q.push(i);
					d[i] = d[v] + 1;
					//printf("%d %d\n",v+1,d[v]);
				}
			}
		}
	}
}

int main()
{
	int u,k,v;
	scanf("%d", &n);
	for(int i=0; i<n; i++)
	{
		scanf("%d%d", &u, &k);
		u -= 1;
		for(int j=0; j<k; j++)
		{
			scanf("%d", &v);
			v -= 1;
			A[u][v] = 1;
		}
		d[u] = -1;
	}
	
	bfs(0);

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

	return 0;
}

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

int n,u,k,v;
int g[102][102];
int d[102];
int sn[102];
int cnt=0;
int x,siz;

queue<int>q;

int main(){
  cin>>n;
  memset(g,0,sizeof(g));
  for(int i=1;i<=n;i++){
    cin>>u>>k;
    d[i]=-1;
    sn[i]=0;
    for(int j=1;j<=k;j++){
      cin>>v;
      g[u][v]=1;
    }
  }
  
  q.push(1);

  while(!q.empty()){

    siz=q.size();
    cout<<siz<<endl;

    for(int i=0;i<siz;i++){
      
      x=q.front();
      q.pop();
      
      sn[x]=1;
      if(d[x]==-1)d[x]=cnt;
      
      for(int j=1;j<=n;j++){
	
	if(g[x][j]==1 && sn[j]==0){
	  q.push(j);
	}
      
      }

    }

    cnt++;

  }

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

  }

}