Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

func (q *Queue) push(v Pair) {
	*q = append(*q, v)
}

func (q *Queue) pop() Pair {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

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

using namespace std;
struct node
{
    node() {};
    node(int i, int n, int* xs) : id {i}, ns {vector<int>(xs, xs+n)}, d {-1} {};
    int id;
    vector<int> ns;
    int d;
};

void bfs(node* xs, int n, int st)
{
    queue<int> que {};
    bool used[n+1];
    for(int i = 0; i <= n; ++i){
        used[i] = false;
    }
    que.push(st);
    xs[st].d = 0;
    used[st] = true;
    while(que.size())
    {
        int p = que.front();
        que.pop();
        vector<int> ns = xs[p].ns;
        for(auto x : ns)
        {
            if(used[x])
            {
                continue;
            }
            xs[x].d = xs[p].d + 1;
            used[x] = true;
            que.push(x);
        }
    }
}

int main()
{
    int n;
    cin >> n;
    node graph[n+1];
    for(int i = 0; i < n; ++i)
    {
        int id;
        int m;
        cin >> id >> m;
        int xs[m];
        for(int j = 0; j < m; ++j)
        {
            cin >> xs[j];
        }
        graph[id] = node(id, m, xs);
    }
    bfs(graph, n, 1);
    for(int i = 1; i <= n; ++i)
    {
        cout << i << ' ' << graph[i].d << endl;
    }
}