Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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;
}