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 <bits/stdc++.h>
using namespace std;

const int MAX=200;
vector<int> T[MAX],state(MAX),d(MAX,-1);
int N;
queue<int> Q;

void BFS(){
    Q.push(0);
    d[0]=0;
    state[0]=1;
    while(Q.size()){
        int a=Q.front();
        for(int i=0;i<T[a].size();i++){
            if(state[T[a][i]]==0){
            Q.push(T[a][i]);
            state[T[a][i]]=1;
            d[T[a][i]]=d[a]+1;
        }
        }
        Q.pop();
    }
    for(int i=0;i<N;i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
}

int main(){

    cin>>N;
    
    for(int i=0;i<N;i++){
        state[i]=0;
    }
    for(int i=0;i<N;i++){
        int x,y;cin>>x>>y;
        x--;
    for(int j=0;j<y;j++){
        int a;cin>>a;
        a--;;
        T[x].push_back(a);
    }
    }
    BFS();
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);
int n, M[N][N];
int D[N];
void bfs(int S) {
	queue<int>Q;
	Q.push(S);
	for (int i = 0; i < n; i++) D[i] = INFTY;
	D[S] = 0;
	int U;
	while (!Q.empty()) {
		U = Q.front(); Q.pop();
		for (int V = 0; V < n; V++) {
			if (M[U][V] == 0) continue;
			if (D[V] != INFTY) continue;
			D[V] = D[U] + 1;
			Q.push(V);
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((D[i] == INFTY) ? (-1) : D[i]) << endl;
	}
}
int main() {
	int U, K, V;

	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) M[i][j] = 0;
	}
	for (int i = 0; i < n; i++) {
		cin >> U >> K;
		U--;
		for (int j = 0; j < K; j++) {
			cin >> V;
			V--;
			M[U][V] = 1;
		}
	}
	bfs(0);
	return 0;
}

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
    ll N;
    cin >> N;
    Graph graph(N);
    rep(i, N) {
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            ll v;
            cin >> v;
            v--;
            graph[u].push_back(v);
        }
    }
    vector<ll> dist(N, -1);
    dist[0] = 0;
    queue<P> que;
    que.push({0, 0});
    while (!que.empty()) {
        P p = que.front();
        que.pop();
        for (ll v: graph[p.first]) {
            if (dist[v] == -1) {
                dist[v] = p.second + 1;
                que.push({v, dist[v]});
            }
        }
    }
    rep(i, N) {
        cout << i + 1 << ' ' << dist[i] << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n;
vector<vector<int>> M(100, vector<int>(100));
vector<int> C(100);
vector<int> D(100);


void bfs(int v) {
  C.at(v) = GRAY;
  D.at(v) = 0;
  queue<int> Q;
  Q.push(v);

  while (Q.size() > 0) {
    int u = Q.front(); Q.pop();
    C.at(u) = BLACK;
    rep(i, n) {
      if (M.at(u).at(i) == 1 && C.at(i) == WHITE){
        Q.push(i);
        C.at(i) = GRAY;
        D.at(i) = D.at(u)+1;
      } 
    }
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    int id, k; cin >> id >> k;
    rep(j, k) {
      int v; cin >> v;
      M.at(i).at(v-1) = 1;
    }
  }
  rep(i, n)D.at(i) = -1;
  bfs(0);
  rep(i,n) cout << i+1 << " " << D.at(i) << endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif


int main(int argc, char **argv){
	int n, u, k, v;
	std::vector<std::vector<int> > G;
	std::vector<int> min_cost;
	std::vector<bool> is_searched;

#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
	//FILE *fp = fopen("test.txt", "r");
#endif

#if INPUT_FROM_FILE
	ifs >> n;
#else
	std::cin >> n;
#endif

	G.resize(n);
	is_searched.resize(n, false);
	min_cost.resize(n, -1);

	for(int i = 0; i < n; i++){
#if INPUT_FROM_FILE
		ifs >> u >> k;
#else
		std::cin >> u >> k;
#endif
		G[u - 1].resize(k);
		for(int j = 0; j < k; j++){
#if INPUT_FROM_FILE
			ifs >> v;
#else
			std::cin >> v;
#endif
			G[u - 1][j] = v - 1;
		}
	}

	min_cost[0] = 0;

	std::queue<int> que;
	
	que.push(0);
	while(!que.empty()){
		int n = que.front(); que.pop();

		for(int i = 0; i < G[n].size(); i++){
			int nn = G[n][i];
			
			if(min_cost[nn] < 0 || min_cost[nn] > min_cost[n] + 1){
				min_cost[nn] = min_cost[n] + 1;
				que.push(nn);
			}
		}
	}

	for(int i = 0; i < n; i++){
		std::cout << i + 1 << " " << min_cost[i] << std::endl;
	}


	return 0;
}