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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <queue>

using namespace std;

const int MAX_N = 100;
int n;
int suji[MAX_N];
int	num[MAX_N];
int adj[MAX_N][MAX_N];
int ans[MAX_N][MAX_N];
int D[MAX_N];

void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;

	while(!Q.empty()){
		int cur = Q.front();
		Q.pop();

		for(int i = 0; i < n; i++){
			if(ans[cur][i] == 1 && D[i] == 0){
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}
}


int main(void){

	cin >> n;
	for(int i = 0; i < n; i++){
		
		cin >> suji[i] >> num[i];

		
		for(int j = 0; j < num[i]; j++){
			cin >> adj[i][j];
			adj[i][j] -= 1;
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			ans[i][j] = 0;
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < num[i]; j++){
			ans[i][adj[i][j]] = 1;
		}
	}

	bfs(0);

	for(int i = 0; i < n; i++){
		cout << i + 1 << " " << D[i] << endl;
	}
	
    return 0;
}

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

using namespace std;

void bfs(int v,vector<vector<int>>& vertex, int d[]);

int depth = 0;

int main(){
    int n;
    cin >> n;
    int num;
    int tmp2;
    int d[n] = {};
    vector<vector<int>> vertex;
    vector<int> tmp;
    for(int i=0;i<n;i++){
        cin >> num;
        cin >> num;
        for(int j=0;j<num;j++){
            cin >> tmp2;
            tmp.push_back(tmp2-1);
        }
        vertex.push_back(tmp);
        tmp.clear();
    }
    bfs(0, vertex, d);
    for(int i=0;i<n;i++){
        cout << i+1 << " " <<  d[i] << endl;
    }
    return 0;
}

void bfs(int v, vector<vector<int>>& vertex, int d[]){
    queue<int> Q;
    d[v] = depth++;
    Q.push(v);
    while(!Q.empty()){
        v = Q.front();
        Q.pop();
        for(int i : vertex[v]){
            if(d[i]==0){
                d[i] = depth;
                Q.push(i);
            }else{
                depth++;
            }
        }
    }
}

C++: #include <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>
#include <numeric>
#include <queue>

using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf("%d", list[i]);
        if ( i + 1 != list.size() ) {
            printf(" ");
        }
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


class Node{
public:
    Node(vector<int> adjacency)
        : adjacency(adjacency) 
    {
        // constructer
    }
    // int dist = std::numeric_limits<int>::max();
    int dist = -1;
    bool done = false;
    vector<int> adjacency;
};

class List{
public:
    List(){
        // 0 番目はダミー
        list.push_back(Node({}));
    }
    void Add(Node&& node){
        this->list.push_back(node);
    }
    void Traverse(){
        this->list[1].dist = 0;
        this->list[1].done = true;
        for (auto each : this->list[1].adjacency) {
            this->list[each].dist = 1;
            this->queue.push(each);
        }
        while (!this->queue.empty()) {
            int front = this->queue.front();
            this->queue.pop();
            if(this->list[front].done) continue;

            for (auto each : this->list[front].adjacency) {
                if(this->list[each].dist == -1){
                    this->list[each].dist = this->list[front].dist + 1;
                }else{
                    this->list[each].dist = std::min(this->list[front].dist + 1, this->list[each].dist);
                }
                this->queue.push(each);
            }
            this->list[front].done = true;
        }
    }
    void Print() {
        for (int i = 1; i < list.size(); i++) {
            printf("%d %d\n", i, list[i].dist);
        }
    }
private:
    vector<Node> list;
    std::queue<int> queue;
    // void _traverse(int index, int dist)
    // {
    //     if (dist < this->list[index].dist) {
    //         this->list[index].dist = dist;
    //     }
    //     for(int i = 0; i < current.adjacency.size(); i++) {
    //         _traverse(current.adjacency[i], dist);
    //     }
        
    //     this->list[index].finished = count++;
    // }
};

void solve() {
    List list;
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        int value;
        scanf("%d", &value);
        int k;
        scanf("%d", &k);
        vector<int> adjacency(k);
        for(int j = 0; j < k; j++){
            int link;
            scanf("%d", &link);
            adjacency[j] = link;
        }
        list.Add(Node(adjacency));
    }
    list.Traverse();
    list.Print();
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    return 0;
}