Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

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

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

C++: /**
 * Bredth first search.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N];

void bfs(int n, int dd) {
	int k = adj[n][0];
	d[n] = dd++;
	for (int j=1;j<=k;j++) {
		bfs(adj[n][j], dd);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
		d[i]=-1;
	}

	bfs(1, 0);

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

	return 0;
}

C++: #include <iostream>
#include <fstream>

#include <cassert>
#include <climits>

#include <string.h>

#include <vector>
#include <map>
#include <stack>
#include <deque>

enum Color
{
    kWhite,
    kGray,
    kBlack,
};

int n;
int start_vertex = 0;
std::vector< std::vector<int> > matrix;
std::vector<Color> c;
std::vector<int> d;

void breadthFirstSearch()
{
    for (int i = 0; i < n;++i)
    {
        c.push_back(kWhite);
        d.push_back(-1);
    }

    std::deque<int> tmp_queue;
    tmp_queue.push_back(start_vertex);
    c[start_vertex] = kGray;
    d[start_vertex] = 0;

    while (! tmp_queue.empty())
    {
        int current = tmp_queue.front();
        tmp_queue.pop_front();
        c[start_vertex] = kBlack;

        std::vector<int> line = matrix[current];
        for (int i = 0; i < line.size(); ++i)
        {
            if (line[i] > 0 && c[i] == kWhite)
            {
                tmp_queue.push_back(i);
                c[i] = kGray;
                d[i] = d[current] + 1;
            }
        }
    }
}

int main()
{
    std::cin >> n;
    for (int i = 0; i < n; ++i)
    {
        std::vector<int> line;
        for (int j = 0; j < n; ++j)
        {
            line.push_back(0);
        }
        matrix.push_back(line);
    }

    for (int i = 0; i < n; ++i)
    {
        int u;
        int k;

        std::cin >> u;
        std::cin >> k;

        for (int j = 0; j < k; ++j)
        {
            int tmp;
            std::cin >> tmp;
            matrix[u - 1][tmp - 1] = 1;
        }
    }

    breadthFirstSearch();

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

    return 0;
}

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

vector<int> bfs(vector<vector<int>> &adj_list)
{
    vector<int> res(adj_list.size(), -1);
    vector<bool> visit(adj_list.size(), 0);
    queue<int> q;
    q.push(0);
    visit[0] = 1;
    res[0] = 0;

    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < adj_list[u].size(); i++)
        {
            int v = adj_list[u][i];
            if (visit[v] == 0)
            {
                visit[v] = 1;
                res[v] = res[u] + 1;
                q.push(v);
            }
        }
    }

    return res;
}

int main()
{
    int n;
    cin >> n;

    vector<vector<int>> adj_list(n);
    for (int i = 0; i < n; i++)
    {
        int id, k;
        cin >> id >> k;
        for (int j = 0; j < k; j++)
        {
            int u;
            cin >> u;
            adj_list[id - 1].push_back(u - 1);
        }
    }

    // for (int i = 0; i < n; i++)
    // {
    //     for (int j = 0; j < adj_list[i].size(); j++)
    //     {
    //         cout << adj_list[i][j] << ", ";
    //     }
    //     cout << endl;
    // }

    vector<int> res = bfs(adj_list);
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << res[i] << endl;
    }

    return 0;
}

C++: /*
?????????: Graph I - Breadth First Search
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C
?????°
19:08 ??°?????????????????????????????£??¨??????????????????????????§???????????????????????£?????????
19:08 ???????????\????????\????????°??????????????????????¢????
19:09 ????????°???????????????????§£
19:13 ??¨???????????????????????£???
19:19 BFS????¨???¶???????¶??????????????????§??¨?????????????????????????????????
19:28 ???????????????????§?????????????????????£????????§????????????????????????????????????????????§????????????????????£????????????
19:29 ?????????????????????????????????????????? 1 ????????????????????????????????????????????????????????¢??¨??????-1 ?????????????????????????????????
*/
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#define INF 114514
using namespace std;

int main()
{
	int n; cin>>n;
	vector< set<int> > graphdat(n+1);  
	for(int i=0;i<n;i++){
		int u; int k; cin >> u >> k;
		set<int> v;
		for(int j=0;j<k;j++){
			int tmp;
			cin >> tmp;
			v.insert(tmp);
		}
		graphdat[u] = v;
	}

	vector<int> distance(n+1);
	distance[1] = 0;
	for(int i=2;i<=n;i++){
		distance[i] = INF;
	}

	queue<int> Q;
	Q.push(1);

	while(!Q.empty()) {
		int cur = Q.front();
		Q.pop(); // pop?????????????????????????????¨?¬???\??§??????
		for(int dest=1; dest<=n; dest++) {
			if(graphdat[cur].find(dest) != graphdat[cur].end()) { // ??????????????????
				if(distance[dest] > distance[cur] + 1) { // ??´??°??§????????????
					distance[dest] = distance[cur] + 1;
					Q.push(dest);
				}
			}
		}
	}

	for(int i=1;i<=n;i++){
		cout << i << " " << (distance[i] == INF ? -1 : distance[i])<< endl;
	}

}