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<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(ll i=0;i<N;++i)
typedef long long int ll;
ll INF = 1LL<<60;

ll N;
vector<vector <int> > M(110,vector<int>(110)); 
bool visit[110];
vector<ll> d(110,INF);

void bfs(int s){
    queue<int> que;
    que.push(s);
    d[s] = 0;

    while(!que.empty()){
        int v = que.front(); que.pop();
        for(int u = 0;u < N;u++){
            if(M[v][u] == 0) continue;
            if(d[u] != INF) continue;
            if(visit[u] == false){
                d[u] = d[v] + 1;
                que.push(u);
            }
        }
    }
}

int main(){
    cin >> N;
    rep(i,N){
        int u,k; cin >> u >> k;
        u--;
        rep(j,k){
            int v; cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0);

    rep(i,N){
        ll dist;
        (d[i] == INF) ? dist = -1 : dist = d[i];
        cout << i+1 << " " << dist << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;


int main(){
    int n;
    cin >> n;
    vector<vector<int> > adj(110);
    int D[110];
    for(int i = 0; i < n; i++){
        int u, k;
        cin >> u >> k; 
        D[u] = -1;
        for(int j = 0; j < k; j++){
            int v;
            cin >> v;
            adj[u].push_back(v);
        }
    }
    queue<int> Q;
    Q.push(1);
    D[1] = 0;
    while(!Q.empty()){
        int cur = Q.front(); 
        Q.pop();
        for(int i = 0; i < adj[cur].size(); i++){
            int dst = adj[cur][i];
            if(D[dst] == -1){
                D[dst] = D[cur]+1;
                Q.push(dst);
            }
        }
    }
    for (int i = 1; i < n+1; i++){
        cout << i << " " << D[i] << endl;
    }
}

C++: #include<iostream>
#include<queue>

using namespace std;
const static int N=100;
const static int INFTY=(1<<21);
int n,M[N][N];
int d[N];

void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=0; i<n; i++){
        d[i]=INFTY;
    }
    d[s]=0;
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int v=0; v<n; v++){
            if(M[u][v]==0){continue;}
            if(d[u]!=0){continue;}
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    for(int i=0; i<n; i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
}

int main(){
    int u,v,k;
    cin>>n;

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            M[i][j]=0;
        }
    }

    for(int i=0; i<n; i++){
        cin>>u>>k;
        u--;
        for(int j=0; j<k; k++){
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}

C++: #include<iostream>
#include<queue>

using namespace std;
static const int N=100;
static const int INFTY=(1<<21);

#define rep(i,n)for(int i=0;i<n;i++)

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

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    rep(i,n)d[i]=INFTY;
    d[s]=0;
    int u;
    while (!q.empty())
    {
        u=q.front();
        q.pop();
        rep(v,n)
        {
            if(M[u][v]==0)continue;
            if (d[v]!=INFTY)continue;
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    rep(i,n)
    {
        cout<<i+1<<" "<<((d[i]==INFTY)?(-1):d[i])<<endl;
    }
}

int main()
{
    int u,k,v;

    cin>>n;
    rep(i,n)
    {
        rep(j,n)M[i][j]=0;
    }
    
    rep(i,n)
    {
        cin>>u>>k;
        u--;
        rep(j,k)
        {
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);

    return 0;
}

C++: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int main(){
    int n;
    cin >> n;

    vector<vector<ll>> g(n);

    rep(i, n){
        int u, k;
        cin >> u >> k;

        rep(j, k){
            int v;
            cin >> v;
            v--;
            g[i].push_back(v);
        }
    }

    queue<int> que;
    que.push(0);

    ll dist[n];
    rep(i, n)dist[i] = -1;
    dist[0] = 0;

    bool visited[n] = {};
    visited[0] = true;

    while(!que.empty()){
        int pos = que.front();

        que.pop();

        rep(i, g[pos].size()){
            int child = g[pos][i];

            if(visited[child])continue;
            visited[child] = true;

            dist[child] = dist[pos] + 1;

            que.push(child);
        }
    }

    rep(i, n)cout << i+1 << " " << dist[i] << endl;
}