Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

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