Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

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

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, u, v, k, l;
int edge[202];
int start[101];
int goal[101];
int c;
int bfs[101];
queue<int> Q;
int main()
{
	cin >> n;
	c=0;
	for(int i=1; i<=n; i++)
	{
		cin >> u;
		start[i]=c;
		cin >> k;
		c+=k;
		goal[i]=c;
		for(int j=start[i]; j<goal[i]; j++)
		{
			cin >> edge[j];
		}
	}
	for(int i=1; i<=n; i++)
	{
		bfs[i]=-1;
	}
	bfs[1]=0;
	Q.push(1);
	while(!Q.empty())
	{
		v=Q.front();
		Q.pop();
		for(int j=start[v]; j<goal[v]; j++)
		{
			l=edge[j];
			if(bfs[l]==-1)
			{
				bfs[l]=bfs[v]+1;
				Q.push(l);
			}
		}
	}
	for(int i=1; i<=n; i++)
	{
		cout << i << ' ' << bfs[i] << endl;
	}
	return 0;
}

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

void calc(const vector<vector<int> >& G, vector<int>& D,
                                        queue<int>& Q, int dim) {
    if (Q.empty()) return;
    queue<int> Q2;
    while (!Q.empty()) {
        int i = Q.front();
        for (int j = 1; j < G[i].size(); j++) {
            if (G[i][j] && D[j] == -1) {
                Q2.push(j);
                D[j] = dim;
            }
        }
        Q.pop();
    }
    calc(G, D, Q2, dim + 1);
}

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

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

    return 0;
}

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

#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];   // 距離で訪問状態（color）を管理する

void bfs(int s) {
    queue<int> q;   // 標準ライブラリのqueueを使用
    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 <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;
}