Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

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

int n, N[100][100];
int d[100];

void bfs(int s) {
    queue<int> q;
    q.push(s);
    for (int i = 0; i < n; i++) d[i] = (1 << 21);
    d[s] = 0;
    int u;
    while(!q.empty()) {
        u = q.front();
        q.pop();
        for (int v = 0; v < n; v++) {
            if (N[u][v] == 0) continue;
            if (d[v] != (1 << 21)) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << ( (d[i] == (1 << 21)) ? (-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++) {
            N[i][j] = 0;
        }
    }
    
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        if (k != 0) {
            for (int j = 0; j < k; j++) {
                cin >> v;
                N[u-1][v-1] = 1;
            }
        }
    }
    bfs(0);
    
    return 0;
}

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

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

int main() {
	int n;
	cin >> n;
	int dist[n];
	vector<int> map[n];
	for (int i = 0; i < n; i++) {
		dist[i] = -1;
		int a, b, c;
		cin >> a >> c;
		for (int j = 0; j < c; j++) {
			cin >> b;
			map[a - 1].push_back(b - 1);
		}
	}
	
	queue<int> q;
	q.push(0);
	dist[0] = 0;
	while (q.size() > 0) {
		int c = q.front(); q.pop();
		for (int i = 0; i < map[c].size(); i++) {
			if (dist[map[c][i]] != -1) continue;
			dist[map[c][i]] = dist[c] + 1;
			q.push(map[c][i]);
		}
	}
	
	for (int i = 0; i < n; i++)
		cout << (i + 1) << " " << dist[i] << endl;
	return 0;
}

C++:  #include<bits/stdc++.h>
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<long long> vlong;
#define _GLIBCXX_DEBUG
#define vpush(a,x) a.push_back(x);
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,u,v,k;
    cin>>n;
    vector<vector<int> > lis(n+5);
    vector<bool> flag(n+5,false);
    rep(i,n){
        cin>>u>>k;
        rep(j,k){
            cin>>v;
            lis[u].push_back(v);
        }
    }
    queue<int> que;
    vector<int> time(n+5,-1);
    int now=0;
    que.push(1);
    time[1]=0;
    while(!que.empty()){
        int nowp=que.front();
        que.pop();
        if(flag[nowp]){
        }
        else{
            flag[nowp]=true;
            rep(i,lis[nowp].size()){
                que.push(lis[nowp][i]);
                if(time[lis[nowp][i]]==-1){
                    time[lis[nowp][i]]=time[nowp]+1;      
                }
            }
        }
    }
    REP(i,1,n+1){
        cout<<i<<' '<<time[i]<<'\n';
    }
    return(0);
}

C++: #include <bits/stdc++.h>
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define FORq(i, m, n) for(int i = m;i <= n;i++)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define PRINTD(n) printf("%d\n",n)
#define DEBUG printf("%s\n","debug")
using namespace std;
 /////////////////////////////////////////////////////
const int V_MAX = 102;
bool adj[V_MAX][V_MAX];
int d[V_MAX] = {};

int main(){
	int n;SCD(n);
	FORq(i,1,n){
		int u,k; SCD2(u,k);
			FORq(j,1,k){
				int v; SCD(v);
				adj[u][v] = true;
			}
		
	}

	
	REP(i,V_MAX) {d[i] = -1;}
	
	queue<int> Q;
	Q.push(1);
	d[1] = 0;
	
	while(!Q.empty()){
		int now = Q.front(); 
		Q.pop();
		int v = d[now];
		FORq(i,1,n){
			if ((adj[now][i] == true) and (d[i] == -1)){
				d[i] = v+1;
				Q.push(i);
			}
		}
	}
	
	FORq(i,1,n){
		printf("%d %d\n",i,d[i]);
	}
	
	
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int adj[105][105] = {0};
int res[105] = {0};
int n;

int main() {
    cin >> n;
    REP(i, n){
        int u, k;
        cin >> u >> k;
        REP(j, k) {
            int tmp;
            cin >> tmp;
            adj[u-1][tmp-1] = 1;
        }
        res[i] = INF;
    }
    res[0] = 0;
    REP(k, 3) REP(i, n) REP(j, n)
        if(adj[i][j] != 0)
            res[j] = min(res[j], res[i]+1);

    REP(i, n){
        if(res[i] != INF)
            cout << i+1 << " " << res[i] << endl;
        else
            cout << i+1 << " -1" << endl;
    }
    return 0;
}

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