Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

C++: #include<bits/stdc++.h>
using namespace std;

static const int N = 100;
//static const int white = 0;
//static const int gray = 1;
//static const int black = 2;
static const int inf = (1 << 30);

int n, m[N][N];
int d[N];
//color[N];

void bfs(int s) {
	queue<int> q;
	for (int i = 0; i < n; i++) {
		//color[i] = white;
		d[i] = inf;
	}
	d[s] = 0;
	q.push(s);
	//color[s] = gray;
	while (!q.empty()) {
		for (int  i = 0; i < n; i++) {
			if (m[s][i] == 1 && d[i] == inf) {
				q.push(i);
				d[i] = d[s] + 1;
			}
		}
	//	color[s] = black;
		q.pop();
		s = q.front();
	}
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			m[i][j] = 0;
		}
	}	
	int u, k, v;
	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);
	for (int i = 0; i < n; i++) {
		if (d[i] == inf)
			cout << i + 1 << " " << -1 << endl;
		else
			cout << i + 1 << " " << d[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define NODE_SIZE 101
#define NIL -1

using namespace std;

int adj[NODE_SIZE][NODE_SIZE];
int depth[NODE_SIZE];
int n, d;

void bfs(int node, int parent){
    depth[node] = depth[parent] + 1;
    for(int i = 1; i <= n; i++){
        if(adj[node][i] && (depth[i] == NIL || depth[i] > depth[node] + 1)){
            bfs(i, node);
        }
    }
}

int main(){
    int u, k, v;

    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> u >> k;
        depth[u] = NIL;
        while(k--){
            cin >> v;
            adj[u][v] = 1;
        }
    }


    depth[0] = NIL;
    bfs(1, 0);
    for(int i = 1; i <= n; i++){
        cout << i << " " << depth[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <stack>
#include <queue>
#include <list>
#include <stdio.h>
#include <algorithm>
#define MAX 100000
using namespace std;
typedef long long ll;
int map[255][255];
int d[255];
bool vis[255];
void bfs(int n,int pos,int distance)
{
	int a[255],j=0;
	vis[pos]=true;
	for(int i=1;i<=n;i++)
	{
		if(map[pos][i]&&!vis[i]){
			a[j++]=i;
			vis[i]=true;
			d[i]=distance;
		} 
	}
	for(int i=0;i<j;i++)
	{
		bfs(n,a[i],distance+1);
	}
}
int main()
{	
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			map[i][j]=0;
		}
	for(int i=0;i<n;i++)
	{
		int cur,k,u;
		cin>>cur>>k;
		while(k--)
		{
			cin>>u;
			map[cur][u]=1;
		}
	}
	for(int i=1;i<=n;i++)
	{
		vis[i]=false;
		d[i]=-1;
	}
	d[1]=0;
	bfs(n,1,1);
	for(int i=1;i<=n;i++)
	{
		cout<<i<<" "<<d[i]<<endl;
	}
	return 0;
}

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

int D[100];
int mat[100][100];

void bfs(int src);

int main() {
  int n;
  for (int i=0;i<100;++i) {
    for (int j=0;j<100;++j)
      mat[i][j] = 0;
    D[i] = -1;
  }
  cin >> n;
  for (int i=0;i<n;++i) {
    int buf1,buf2;
    cin >> buf1 >> buf2;
    for (int j=0;j<buf2;++j) {
      int buf3;
      cin >> buf3;
      mat[i][buf3-1] = 1;
    }
  }
  bfs(0);
  for (int i=0;i<n;++i)
    cout << i+1 << " " << D[i] << endl;
}

void bfs(int src) {
  // cout << "bfs root = " << src << endl;
  queue<int> Q; // 整数を管理するキューの定義
  Q.push(src);
  D[src] = 0; // 出発点
  while (!Q.empty()) {
    int cur = Q.front(); // 先頭要素を取り出す
    Q.pop();
    // 動作確認用表示
    // cout << "visiting " << cur << " " << D[cur] << endl;
    for (int dst=0;dst<100;++dst) { // 各行き先 dst に対して
      if (mat[cur][dst]==1&&D[dst]==-1) {
	// cur から dst に辺があり, dst が未訪問なら
	D[dst] = D[cur]+1;
	Q.push(dst); // dst を訪問先に加える
      }
    }
  }
}

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