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<queue>
#include<algorithm>
#define INF 999999999
using namespace std;

int res[101];
int G[101][101] = {0};

int main() {
    int n, u, v, k;
    std::queue<int> Q;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> u >> k;
        for (int j = 0; j < k; j++) {
            std::cin >> v;
            G[u - 1][v - 1] = 1;
        }
    }

    for (int i = 0; i < n; i++) res[i] = INF;

    Q.push(0);
    res[0] = 0;

    while (! Q.empty()) {
        u = Q.front();
        for (int i = 0; i < n; i++) {
            if (G[u][i]) {
                Q.push(i);
                res[i] = std::min(res[i], res[u] + 1);
                G[u][i] = 0;
            }
        }
        Q.pop();
    }

    for (int i = 0; i < n; i++) {
        if (res[i] != INF) std::cout << i + 1 << " " << res[i] << "\n";
        else std::cout << i + 1 << " -1\n";
    }

    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;

const int MAX = 105;
int is_visited[MAX];
int dist[MAX];
int am[MAX][MAX];
queue<int> Q;
int t, n;
int ns[MAX];

int next(int u) {
  for (int i = ns[u]; i < n; i++) {
    ns[u] = i + 1;
    if (am[u][i] == 1) {
      return i;
    }
  }
  return -1;
}

void bfs_visit(int r) {
  int current = r;

  dist[current] = 0;
  is_visited[r] = 1;
  for (int i = 0; i < n; i++) {
    if (am[current][i] == 1) {
      Q.push(i);
      dist[i] = dist[current] + 1;
      is_visited[i] = 1;
    }
  }

  while (!Q.empty()) {
    current = Q.front();
    Q.pop();
    for (int i = 0; i < n; i++) {
      if (am[current][i] == 1 && is_visited[i] == 0) {
        Q.push(i);
        dist[i] = dist[current] + 1;
        is_visited[i] = 1;
      }
    }
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      am[u-1][v-1] = 1;
    }
  }

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

  bfs_visit(0);

  for (int i = 0; i < n; i++) {
    if (dist[i] == MAX) {
      dist[i] = -1;
    }
    cout << i+1 << " " << dist[i] << endl;
  }
}

C++: #define N 1000
#define M -1
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int m[N][N] = {};
int d[N] = {};
int n;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++)d[i] = M;
	int j;
	for (int i = 0; i < n; i++) {
		int k;
		int u;
		int v;
		cin >> u;
		cin >> k;
		for (j = 0; j < k; j++) {
			cin >> v;
			m[u][v] = 1;
		}
	}
	queue<int> q;
	q.push(1);
	d[1] = 0;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		if (d[u] != M) {
			for (int i = 1; i <= n; i++) {
				if (m[u][i]) {
					if (d[i] == M) {
						d[i] = d[u] + 1;
						q.push(i);
						//cout << "a" << endl;
					}
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>

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



using namespace std;




int main() {
	
	int n;
	cin>>n;
	vector<vector<int>>t(n,vector<int>(0));
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
		for(int j=0;j<b;j++){
			int x;
			cin>>x;x--;t[i].push_back(x);
		}
	}
	queue<int>r;
	r.push(0);
	int g[105]={1000000};
	for(int i=0;i<n;i++){
		g[i]=10000000;
	}
	g[0]=0;
	set<int>p;
	p.insert(0);
	while(r.size()||p.size()<n){
		if(!r.size()){
			for(int i=0;i<n;i++){
				if(g[i]==10000000){
					g[i]=-1;
					p.insert(i);
					//r.push(i);
				}
			}
		}
		else{
		int v=r.front();
		for(int i=0;i<t[v].size();i++){
			r.push(t[v][i]);
			p.insert(t[v][i]);
			g[t[v][i]]=min(g[t[v][i]],g[v]+1);
			
		}
		r.pop();
		
		}
	}
	for(int i=0;i<n;i++)
		cout<<i+1<<" "<<g[i]<<endl;

	
	return 0;
}

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

using namespace std;

const int WHITE = 0, GREY = 1, BLACK = 2;
const int INF = 1000000;

struct BFS {
    int n;
    vector<vector<int>> g;
    vector<int> parent;
    vector<int> distance;
    vector<int> color;
    
    BFS (int n) : n(n) {
        g.assign(n, vector<int>(0));
        parent.assign(n, -2);
        distance.assign(n, INF);
        color.assign(n, WHITE);
    }
    
    void addEdge (int u, int v) {
        g[u].push_back(v);
    }
    
    void bfs (int s) {
        queue<int> q;
        q.push(s);
        parent[s] = -1;
        distance[s] = 0;
        color[s] = GREY;
        while (!q.empty()) { //u = current, v = neighbors
            int u = q.front();
            q.pop();
            for (int v : g[u]) {
                if (color[v] == WHITE) {
                    q.push(v);
                    parent[v] = u;
                    distance[v] = distance[u] + 1;
                    color[v] = GREY;
                }
            }
            color[u] = BLACK;
        }
    }
    
    void printResults () {
        for (int i = 0; i < n; i++) {
            int dist = distance[i];
            if (dist == INF) {
                dist = -1;
            }
            cout << i + 1 << " " << dist << endl;
        }
    }
};

int main() {
    
    int n;
    cin >> n;
    BFS bfs(n);
    
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            bfs.addEdge(u - 1, v - 1);
        }
    }
    
    bfs.bfs(0);
    bfs.printResults();
    
    return 0;
}