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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
 
#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
 
#include <functional>
#include <cassert>
 
typedef long long ll;
using namespace std;
 
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
 
#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010
 
 
int main(){
  int n;
  vector<int> G[200];
  
  scanf("%d", &n);
 
  for(int i=0;i<n;i++){
    int u, k, v;
 
    scanf("%d%d", &u, &k);
    u--;
     
    for(int j=0;j<k;j++){
      scanf("%d", &v);
      v--;
      G[u].push_back(v);
    }
  }
 
  queue<int> q1, q2;
  bool visited[200] = {};
  int ans[200];
 
  for(int i=0;i<n;i++) ans[i] = -1;
   
  q1.push(0);
 
  for(int i=0;q1.size();i++){
 
    while(q1.size()){
      int p = q1.front();
      q1.pop();
 
      if(visited[p]) continue;
      visited[p] = true;
 
      ans[p] = i;
       
      for(int j=0;j<G[p].size();j++){
        q2.push(G[p][j]);
      }
    }
 
    q1 = q2;
    q2 = queue<int>();
  }
 
  for(int i=0;i<n;i++)
    printf("%d %d\n", i+1, ans[i]);
   
  return 0;
}

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

int main(){
  queue<int> q;
  int n,k,u,max=0;
  int g[101]={},f[101];
  int d[101][101]={};
  int di;
  cin>>n;
  for(int i=0;i<101;i++) f[i]=-1;
  f[1]=0;
  g[1]=1;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    if(max<k) max=k;
    for(int j=0;j<k;j++){
      cin>>di;
	d[u][j]=di;
    }
  }

  q.push(1);

  int a,c[101]={};
  while(!q.empty()){
 a=q.front();
    for(int i=0;i<=max;i++){

      if( d[a][i]>1 && g[d[a][i]]==0){
	g[d[a][i]]=1;
	q.push(d[a][i]);
	 c[d[a][i]]=c[a]+1;
      }
    }
      q.pop();
  }
  for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
queue<int> qu;
int v[101][101];
int dis[101], n;
int d = 0;
int tf;
inline void b1(int t) {
	for (int i = 1; i <= n; i++) {
		if (v[t][i]) {
			tf = 1;
			qu.push(i);
			dis[i] = d + 1;
			for (int j = 1; j <= n; j++)
				v[j][i] = 0;
		}
	}
}
inline void b2() {
	tf = 0;
	for (int i = 1; i <= n; i++)
		if (dis[i] == d) {
			b1(i);
			qu.pop();
		}
	d++;
}
inline void bfs() {
	tf = 1;
	b1(1);
	d++;
	qu.pop();
	while (tf) {
		b2();
	}
}
int main() {
	int u, k, s;
	cin >> n;
	for (int i = 2; i <= n; i++) {
		dis[i] = -1;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			v[i][j] = 0;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u;
		cin >> k;
		for (int j = 0; j < k; j++) {
			cin >> s;
			v[u][s] = 1;
		}
	}
	qu.push(1);
	for (int i = 1; i <= n; i++)
		v[i][1] = 0;
	bfs();
	for (int i = 1; i <= n; i++) {
		cout << i << " " << dis[i] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int n;
vector<int> a[100];
int dist[100];
const int INF = 100000000;

int main() {
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    rep(i, y) {
      int z;
      cin >> z;
      a[x-1].push_back(z-1);
    }
  }

    rep(i, n) {
      dist[i] = INF;
    }
    dist[0] = 0;

    queue<int> que;

    que.push(0);
    while(que.size()) {
      int p = que.front();
      que.pop();

      for(int i = 0; i < a[p].size(); i++) {
        if(dist[a[p][i]] > dist[p] + 1) {
          dist[a[p][i]] = dist[p] + 1;
          que.push(a[p][i]);
        }
      }
    }

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

  return 0;
}

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

static const int N = 100;
static const int INF = (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] = INF;
	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] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((d[i] == INF) ? (-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--; // 0オリジン
		for (int j = 0; j < k; j++) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}

	bfs(0);
	
	return 0;
}