Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using qi = queue<int>;
using Graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)

qi que;
vi dist;

void bfs(Graph &G) {
    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for(int next_v : G[v]) {
            if (dist[next_v] != -1) continue;
            que.push(next_v);
            dist[next_v] = dist[v] + 1;
        }
    }
}

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

    int n; cin >> n;
    Graph G(n);
    REP(i, n) {
        int u; cin >> u;
        int k; cin >> k;
        REP(j, k) {
            int v; cin >> v;
            G[i].push_back(v-1);
        }
    }

    que.push(0);
    dist.assign(n, -1);
    dist[0] = 0;
    bfs(G);

    REP(i, n) {
        cout << (i+1) << " " << dist[i] << "\n";
    }
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == u) return 0;
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d + 1, d_min);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
vector<vector<int> >vec;
int n;
vector<int>d;
void bfs(int s) {
	queue<int>q;
	q.push(s);
	d[s] = 0;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		rep(v, 0, vec[u].size()) {
			if (d[vec[u][v]] != INF)continue;
			d[vec[u][v]] = d[u] + 1;
			q.push(vec[u][v]);
		}
	}
	rep(i, 0, n) {
		cout << i + 1 << " " << d[i] << endl;
	}
}
signed main() {
	cin >> n; vec = vector<vector<int> >(n); d = vector<int>(n, INF);
	rep(i, 0, n) {
		int u, k; cin >> u >> k; u--;
		rep(j, 0, k) {
			int v; cin >> v; v--;
			vec[u].push_back(v);
		}
	}
	bfs(0);

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
	int n;
	cin >> n;
	vector<int> G(n,-1);
	vector<vector<int>> vs(n);
	int u,k,v;
	for(int loop=0;loop<n;loop++){
		cin >> u >> k;
		for(int loop2=0;loop2<k;loop2++){
			cin >> v;
			vs[u-1].push_back(v-1);
		}
	}
	queue<pair<int,int>> Q;
	Q.push(make_pair(0,0));
	G[0]=0;
	int lk,dist;
	while(!Q.empty()){
		lk = Q.front().first;
		dist = Q.front().second;
		Q.pop();
		for(int loop=0;loop<vs[lk].size();loop++){
			if(G[vs[lk][loop]]==-1){
				G[vs[lk][loop]]=dist+1;
				Q.push(make_pair(vs[lk][loop],dist+1));
			}
		}
	}
	for(int loop=0;loop<n;loop++){
		cout << loop+1 << " " << G[loop] << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int n,g[100][100],f[100],c1[100],c2[100];
void bfs(int now,int far){
  for(int i=0;i<n;i++) if(g[now][i]){
      if(f[i]<far+1){
        c1[i]=1;
        continue;
      }
      f[i]=far+1;
    }
  for(int i=0;i<n;i++) if(g[now][i]&&c1[i]!=1) bfs(i,far+1);
}
int main(){

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    if(i) f[i]=1000;
    cin>>a>>b;
    for(int j=0;j<b;j++){
      cin>>tmp;
      g[a-1][tmp-1]=1;
    }
  }

  bfs(0,0);

  for(int i=0;i<n;i++) if(f[i]==1000) f[i]=-1;
  for(int i=0;i<n;i++) cout<<i+1<<" "<<f[i]<<endl;

  return 0;

}