Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

var infty = 1000000000

type nodeKey int

type DirectedGraphNode struct {
	key       nodeKey
	adjacents []*DirectedGraphNode
	visited   bool
	d         int
	parent    *DirectedGraphNode
}

type DirectedGraph struct {
	nodes []DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(l, r nodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, &g.nodes[r])
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]DirectedGraphNode, nNodes)
	for iNodes := 0; iNodes < nNodes; iNodes++ {
		graph.nodes[iNodes].key = nodeKey(iNodes)

		graph.nodes[iNodes].visited = false
		graph.nodes[iNodes].d = infty
		graph.nodes[iNodes].parent = nil
	}
	return &graph
}

type QueueEntry struct {
	node *DirectedGraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *DirectedGraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

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

func fillDistancesByBreadthFirstSearch(g *DirectedGraph, startNode *DirectedGraphNode) {
	startNode.visited = true
	startNode.d = 0

	queue := Queue{}
	queue.enqueue(startNode)
	for !queue.isEmpty() {
		u := queue.dequeue().node
		for _, adjacent := range u.adjacents {
			if !adjacent.visited {
				adjacent.visited = true
				adjacent.d = u.d + 1
				adjacent.parent = u
				queue.enqueue(adjacent)
			}
		}
		u.visited = true
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	g := newDirectedGraph(nNodes + 1)

	for iNode := 0; iNode < nNodes; iNode++ {
		nodeKeyLeft := nodeKey(nextInt())
		nAdj := nextInt()
		for iAdj := 0; iAdj < nAdj; iAdj++ {
			nodeKeyRight := nodeKey(nextInt())
			g.setAdjacency(nodeKeyLeft, nodeKeyRight)
		}
	}

	// fmt.Println(g)
	fillDistancesByBreadthFirstSearch(g, &g.nodes[1])
	// fmt.Println(g)

	for iNode := 1; iNode <= nNodes; iNode++ {
		node := g.nodes[iNode]
		distance := node.d
		if distance == infty {
			distance = -1
		}
		fmt.Println(node.key, distance)
	}
}

C++: #include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>
#include <ios>
#include <iomanip>
using namespace std;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define print(x) cout<<(x)<<'\n'
#define print_vec(v) rep(i,v.size()){if(i!=0)cout<<" ";cout<<v[i];}cout<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<'\n'
#define pb push_back
#define mp make_pair
#define MOD 1000000007 //10^9+7
#define fi first
#define sc second
using ll=long long;
using pi=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
using vstr=vector<string>;

void solve() {
    int n; cin >> n;
    vector<vi> to(n);
    int u,k,v;
    rep(i,n){        
        cin >> u >> k;
        u--;
        rep(j,k){
            cin >> v;
            to[u].pb(--v);
        }
        // DEBUG_VEC(to[u]);
    }

    vi dist(n,-1);
    dist[0] = 0;
    queue<int> que;
    que.push(0);
    while(!que.empty()){
        int root = que.front();
        que.pop();
        // DEBUG(root);
        // DEBUG_VEC(to[root]);
        for(auto leaf:to[root]){
            if(dist[leaf] != -1) continue;
            dist[leaf] = dist[root]+1;
            que.push(leaf);
        }
    }

    rep(i,n){
        cout << i+1 << " " << dist[i] << '\n';
    }
}
 
int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
    //int q; cin >> q;
	//while (q--)
		solve();
    return 0;
}

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

const int Max = 100;
const int white = 0;
const int black = 1;
int M[Max][Max];
int d[Max], nt[Max], color[Max];
int n;
queue<int> Q;

int next(int u){
    for(int v=nt[u]; v<n; v++){
        nt[u] = v + 1;
        if(M[u][v]) return v;
    }
    return -1;
}

void bfs_visit(int u){
    for(int i=0; i<n; i++){
        nt[i] = 0;
        d[u] = -1;
    }

    color[u] = black;
    d[u] = 0;
    Q.push(u);
    while(!Q.empty()){
        int u = Q.front();
        int v = next(u);
        if(v != -1){
            if(color[v] == white){
                color[v] = black;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        } else {
            Q.pop();
        }
    }
}

int main(){
    int u, k, v;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        color[i] = white;
    }
    for(int i=0; i<n; i++){
        cin >> u >> k;
        u--;
        for(int i=0; i<k; i++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs_visit(0);

    for(int i=0; i<n; i++){
        cout << i+1 << " " << d[i] << endl;
    }
    
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
#include <utility>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i, s, n) for(int i=(s); i<=(n); ++i)
#define outl(x) cout<< (x) << '\n'
#define fi first
#define se second
#define mkp make_pair

#define INF 0x3f3f3f3f
#define NIL -1
using namespace std;

int N;
vector <int> G[102];
int far[102];

void slv()
{
   queue <int> que;
   que.push(1);
   memset( far, NIL, sizeof(far) );
   far[1] = 0;

   while(!que.empty()){
      const int u = que.front();
      que.pop();
      rep(i, G[u].size()){
         const int next = G[u][i];
         if( far[next] == NIL ){
            far[next] = far[u] + 1;
            que.push( next );
         }
      }
   }

   reps(i, 1, N)
      cout << i <<  ' ' <<  far[i]  << '\n';
   return;
}

signed main()
{
   cin.tie(0), ios::sync_with_stdio(false);
   cin >> N;

   reps(i, 1, N){
      int u, k, a;
      cin >> u >> k;
      while(k--){
         cin >> a;
         G[u].push_back(a);
      }
   }

   slv();
   return (0);
}