Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: 
/*
Problem

Category
BFS

URL:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=ja

考察:


Date: 2020/04/14
*/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int n, k;
vector<vector<int>> AL;
vector<int> D;


/* ======================== */

signed main(){
    getInput();
    solve();
    return 0;
}

void getInput() {
    cin>>n;
    D = vector<int>(n, 1e9);
    for(int i=0; i<n; ++i) {
        int g, k;
        cin>>g>>k;
        vector<int> al;
        for(int j=0; j<k; ++j) {
            int a;
            cin>>a;
            al.push_back(a-1);
        }
        AL.push_back(al);
    }
    
}


void bfs(int gid, int d) {
    if(d >= D[gid]) return;
    D[gid] = d;
    for(int child: AL[gid]) {
        bfs(child, d + 1);
    }
}

void solve() {
    // print(AL);
    bfs(0, 0);
    for(int i=0; i<n; ++i) {
        if (D[i] == 1e9) D[i] = -1;
        cout<<i+1<<" "<<D[i]<<endl;
    }
}

C++: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#define ll long long
#define ALL(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define pint_int 2000000003
using namespace std;

class BFS {
public:
    vector<vector<int>> Graph_as_list;

    void emplace_back(vector<int> list) {
        Graph_as_list.push_back(list);
    }
    vector<int> Search(int u) {// search from node u
        vector<int> dist_from_u(Graph_as_list.size(), pint_int);
        queue<int> que;

        que.push(u);
        dist_from_u[u] = 0;
        while (!que.empty()) {
            int u = que.front(); que.pop();
            for (int next_node : Graph_as_list[u]) {
                if (dist_from_u[u] + 1 < dist_from_u[next_node]) {
                    dist_from_u[next_node] = dist_from_u[u] + 1;
                    que.push(next_node);
                }
            }
        }
        for (int& elem : dist_from_u) {
            if (elem == pint_int) elem = -1;
        }
        return dist_from_u;
    }
};

int main() {
    int n; cin >> n;
    BFS bfs;
    for (int i = 0; i < n; i++) {
        int u, k; cin >> u >> k; u--;
        vi temp;
        for (int j = 0; j < k; j++) {
            int v; cin >> v; v--;
            temp.push_back(v);
        }
        bfs.emplace_back(temp);
    }
    vi ans = bfs.Search(0);
    for (int i = 0; i < ans.size(); i++) {
        cout << i + 1 << " " << ans[i] << endl;
    }

}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1LL << 60;
const int MAX = 1000000010;
const ll MOD = 1000000007;
const double PI = acos(-1);

int main() {
    int N; cin >> N;
    vector<vector<int>> G(N);
    for (int i = 0; i < N; i++) {
        int u, k; cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            int v; cin >> v;
            v--;
            G[u].push_back(v);
        }
    }

    queue<int> que;
    vector<int> dist(N, -1);
    dist[0] = 0;
    que.push(0);

    while (!que.empty()) {
        int u = que.front(); que.pop();
        
        for (auto nv : G[u]) {
            if (dist[nv] != -1) continue;

            dist[nv] = dist[u] + 1;
            que.push(nv);
        }
    }

    for (int i = 0; i < N; i++) {
        cout << i + 1 << " " << dist[i] << endl;
    }

    return 0;
}

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

int G[110][110];
int N,num,lines,tar;
int D[1000];
int ret[1000];

void bfs(int src);

int main() {
	for (int i=0;i<110;i++) {
		for(int j=0;j<110;j++) {
			G[i][j] = 0;
		}
	}
	for(int i=0;i<1000;i++) {
		D[i] = 100;
	}
	cin >> N;
	for(int i=0;i<N;i++) {
		cin >> num >> lines;
		for(int j=0;j<lines;j++) {
			cin >> tar;
			G[num][tar] = 1;
		}
	}
	/*
	for(int i=0;i<num+1;i++) {
		for(int j=0;j<num+1;j++) {
			cout << G[i][j] << " ";
		}
		cout << endl;
	}
	*/
	bfs(1);
	for(int i=1;i<num+1;i++) {
		if((ret[i] == 0) && (i>1)) ret[i] = -1;
		cout << i << " " << ret[i] << endl;
	}

}
void bfs(int src) {
	int cur,dst;
	//cerr << "bfs root = " << src << endl;
	queue<int> Q; // ??´??°????????????????????\????????????
	Q.push(src);
	D[src] = 0; // ?????????
	while (! Q.empty()) {
		int cur = Q.front(); // ??????????´????????????????
		Q.pop();
// ???????¢??????¨??¨???
		ret[cur] = D[cur];
		//cerr << cur << " " << D[cur] << endl;
		for (int dst=1;dst<N+1;dst++) { // ???????????? dst ????????????
			if ((G[cur][dst] == 1) && (D[dst] == 100)) { // cur ?????? dst ??????????????????dst ???????¨??????????
				D[dst] = D[cur]+1; //
				Q.push(dst); // dst ????¨???????????????????
			}
		}
	}
}