Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <iomanip>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 100000
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
vector<vector< int> > G;
int N;
vector<int> flag;
vector<int> d;
queue<int> que;

void bfs(int k){
    if(flag[k] == -1){
        que.push(k);
        flag[k] = 0;
        while(!que.empty()){
            int now = que.front(); que.pop();
            if(G[now].size() != 0){
                REP(i,G[now].size()){
                    if(flag[G[now][i]] == -1){
                        flag[G[now][i]] = flag[now] + 1;
                        que.push(G[now][i]);
                    }
                }
            }
        }
    }
}

int main(){
    int n;
    int time = 0;

    
    cin >> n;
    G.resize(n);
    d.resize(n);//最初に発見された時刻
    flag.resize(n,-1);
    REP(i,n){
        int u,k; cin >>u>>k;
        u--;
        if(k != 0){
        REP(j,k){
            int a; cin >>a;
            G[u].push_back(a);
            G[u][j]--;
        }
        }
    }
    //time = 0;
    bfs(0);

    REP(i,n){
        cout << i + 1 << " " << flag[i] << endl;
    }
}