Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 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;
}