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 <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1e9;
typedef pair<int, int> P;
int main()
{
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    rep(i, N) {
        int u, k;
        cin >> u >> k;
        rep(j, k) {
            int v;
            cin >> v;
            G[u - 1].push_back(v - 1);
        }
    }

    vector<int> d(N, INF);
    d[0] = 0;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int v = que.front(); que.pop();
        for (auto to : G[v]) {
            if (d[to] != INF) continue;
            d[to] = d[v] + 1;
            que.push(to);
        }
    }

    rep(i, N) {
        cout << i + 1 << " " << (d[i] == INF ? -1 : d[i]) << endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int main()
{
    int n;
    scanf("%d", &n);
    vector<vector<int>> a(n, vector<int>(0, 0));
    vector<int> b(n,-1);
    b[0] = 0;
    for (int i = 0, u, k; i < n; i++)
    {
        scanf("%d %d", &u, &k);
        u--;
        for (int j = 0, v; j < k; j++)
        {
            scanf("%d", &v);
            a[u].push_back(--v);
        }
    }
    queue<int> c;
    c.push(0);
    for (int t; !c.empty(); )
    {
        t = c.front();
        c.pop();
        for (int i = 0; i < a[t].size(); i++)
        {
            if (b[a[t][i]] == -1)
            {
                c.push(a[t][i]);
                b[a[t][i]] = b[t] + 1;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        printf("%d %d\n", i + 1, b[i]);
    }
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>
#include <stdio.h>
using namespace std;
int n,m,l,p;
int a[100][100];
int D[100];
void bfs( int src) {
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while( ! Q.empty() ) {
		int cur = Q.front();
		Q.pop();
		for(int dst = 0; dst < n; dst++ ) {
			if( D[dst] < 0 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cerr << i+1 <<  " " << D[i] <<  endl;
	}
}

int main() {
	cin >> n;
	for ( int i = 0;i < n;i++) {
		D[i] = -1;
		for(int j= 0; j < n;j++) {
			a[i][j] = 0;
		}
	}
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
	bfs(0);
}

C++: #include <iostream>
using namespace std;
#define INF 9999999

int dis[101]; //距離を格納
bool check[101];//訪問済みチェック
int main(){
  int n;
  cin>>n;
  fill(dis,dis+101,INF);
  dis[1]=0;
  check[1]=true;
  for(int i=0;i<n;i++){
    int u,k,v;
    cin>>u>>k;
    for(int j=1;j<=k;j++){
      cin>>v;
      if(check[v]){//訪問済みならば
        continue;
      }else{
        check[v]=true;
      }
      dis[v]=dis[u]+1;
    }
  }

  //表示部
  for(int i=1;i<=n;i++){
    cout<<i<<" ";
    if(dis[i]==INF)cout<<-1<<"\n";
    else  cout<<dis[i]<<"\n";
  }
}

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

int n;
int aa[max][max];
int ans[max];
int color[max];
int p[max];
queue<int> q;

void bfs(int u) {
	for (int i = 0; i < n; i++) {
		if (aa[u - 1][i] == 1 && color[i] != 1) {
			q.push(i + 1);
			color[i] = 1;
			p[i] = u;
		}
	}
	while (!q.empty()) {
		int x = q.front(); q.pop();
		ans[x - 1] = ans[p[x-1]-1] + 1;
		bfs(x);
	}
}

int main()
{
	for (int i = 0; i < max; i++) {
		ans[i] = 0;
		color[i] = 0;
		p[i] = -1;
	}
	cin >> n;
	for (int i = 0; i < max; i++) {
		for (int j = 0; j < max; j++) {
			aa[i][j] = 0;
		}
	}

	for (int i = 0; i < n; i++) {
		int id;
		cin >> id;
		int num;
		cin >> num;
		for (int j = 0; j < num; j++) {
			int num1;
			cin >> num1;
			aa[id - 1][num1 - 1] = 1;
		}
	}

	bfs(1);

	for (int i = 0; i < n; i++) {
		cout << i + 1 << " ";
		if (i == 0) cout << "0" << endl;
		else if (ans[i] == 0) cout << "-1" << endl;
		else cout << ans[i] << endl;
	}

	return 0;
}