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 <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
bool 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++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			a[i][j] = 0;
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
		bfs(0);
	
}

C++: #include "bits/stdc++.h"
#include <unordered_set>

#define _CRT_SECURE_NO_WARNINGS

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1; i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

using namespace std;

const int INF = 1000000000;
const double PI = 3.1415926535897932384626433832795;

// N, E, S, W
const int dx[4] = { -1, 0,  1,  0 };
const int dy[4] = { 0, 1,  0, -1 };

using LL = long long int;
using LD = long double;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<LL, LL>;
using vi = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vvi = vector<vi>;
using vll = vector<LL>;

//==============================================
int n;
vi D;
vvi M;

void print() {
	REP3(i, 1, n) {
		cout << i << " " << ((D[i] == INF) ? -1 : D[i]) << "\n";
	}
}

void antBfs() {
	D[1] = 0;
	queue<int> q;
	q.push(1);

	int u;
	while (!q.empty()) {
		u = q.front(); q.pop();
		REP3(i, 1, n) {
			if (M[u][i] == 0) continue;
			if (D[i] != INF) continue;
			D[i] = D[u] + 1;
			q.push(i);
		}
	}

	print();
}

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

	cin >> n;

	D.resize(n + 1, INF);
	M.resize(n + 1, vi(n + 1));

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

	antBfs();
	return 0;
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
//#define Kongxiangzhouye
int n;
int a[123][123];
int weight[123];

void bfs(){
	queue<int> q;
	q.push(1);
	weight[1]=0;
	while(!q.empty()){
		int now=q.front();
		q.pop();
		for(int i=1;i<=n;i++){
			if(a[now][i]){
				if(!weight[i]){
					q.push(i);
					weight[i]=weight[now]+1;
				}
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	while(cin>>n){
		memset(a,0,sizeof(a));
		for(int i=0;i<n;i++){
			int t,m,te;
			cin>>t>>m;
			for(int i=0;i<m;i++){
				cin>>te;
				a[t][te]=1;
			}
		}
		bfs();
		for(int i=1;i<=n;i++)
			cout<<i<<" "<<weight[i]<<endl;
	}
	return 0;
}

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

using namespace std;

int main() {
	int n;
	scanf("%d", &n);

	bool G[n + 1][n + 1];
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j)
			G[i][j] = false;
	}

	for (int i = 0; i < n; ++i) {
		int u, k;
		scanf("%d %d", &u, &k);
		for (int j = 0; j < k; ++j) {
			int v;
			scanf("%d", &v);
			G[u][v] = true;
		}
	}

	int d[n + 1];
	for (int i = 0; i <= n; ++i)
		d[i] = -1;

	int color[n + 1];
	int WHITE = 0;
	int GRAY = 1;
	int BLACK = 2;
	for (int i = 0; i <= n; ++i)
		color[i] = WHITE;

	queue<int> Q;
	Q.push(1);
	d[1] = 0;
	color[1] = GRAY;

	while (Q.size() != 0) {
		int v = Q.front();
		for (int i = 1; i <= n; ++i) {
			if (G[v][i] && color[i] == WHITE) {
				Q.push(i);
				color[i] = GRAY;
				d[i] = d[v] + 1;
			}
		}
		Q.pop();
		color[v] = BLACK;
	}
	for (int i = 1; i <= n; ++i) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<queue>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define MAX 105
using namespace std;
int N;
//int M[MAX][MAX];
vector<int> V[MAX];
int D[MAX];
void BFS(int u)
{
	queue<int> Q;
	Q.push(u);
	D[u]=0;
	while(!Q.empty())
	{
		int i=Q.front();
		Q.pop();
		/*FOR(j,1,N)
			if(M[i][j]&&D[j]==MAX)
			{
				Q.push(j);
				D[j]=D[i]+1;
			}
		*/
		for(int j=0;j<V[i].size();j++)
			if(D[V[i][j]]==MAX)
			{
				Q.push(V[i][j]);
				D[V[i][j]]=D[i]+1;
			}
	}
	FOR(i,1,N)
		printf("%d %d\n",i,(D[i]==MAX)?-1:D[i]);
		//cout<<i<<" "<<((D[i]==MAX)?(-1):D[i])<<"\n";
}
int main()
{
	//memset(M,0,sizeof(M));
	scanf("%d",&N);
	FOR(i,1,N)
		D[i]=MAX;
	int u,k,v;
	FOR(i,1,N)
	{
		scanf("%d %d",&u,&k);
		FOR(j,1,k)
		{
			scanf("%d",&v);
			V[u].push_back(v);
		}
	}
	BFS(1);
	return 0;
}