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>
#define _GLIBCXX_DEBUG
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

using Graph = vector<vector<int>>;

int main(){
int n;
cin >> n;
Graph G(n+1);
rep(i,n)
{
  int v;
  cin>>v;
  int k;
  cin >> k;
  rep(j,k)
  {int vt;
   cin >>vt;
   G[v].push_back(vt); 
  }
}

vector<int> dist(n+1,-1);
queue<int> que;

dist[1]=0;
que.push(1);

while (!que.empty())
{
  int v = que.front();
  que.pop();
  //cout << G[1].size()<<endl;
  rep(i,G[v].size())
  { int newv =G[v][i];
   if(dist[newv]==-1)
    {que.push(newv);
     dist[newv]=dist[v]+1;}
  }

}

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

}

return 0;
}
//

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

#define N 100
#define 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;
  int u;
  while ( !q.empty() ) {
    u = q.front(); q.pop();
    for ( int v = 0; v < n; v++ ) {
      if ( M[u][v] == 0 ) continue;
      if ( d[v] != INFTY ) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for ( int i = 0; i < n; i++ ) {
    cout << i + 1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

C++: #include<cstdio>
#include<queue>
using namespace std;
static const int INF=(1<<21);
static const int MAXN=101;
struct p{
	int d;
	int cnt;
};
int main()
{
	int n,m[MAXN][MAXN],i,j,u,k,v,l,o,c=1,t[MAXN];
	p e,f;
	queue<p> q;
	bool used[MAXN]={};
	scanf("%d",&n);
	for(i=1;i<n+1;i++){
		for(j=1;j<n+1;j++){
			m[i][j]=INF;
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		for(j=0;j<k;j++){
			scanf("%d",&v);
			m[u][v]=1;
		}
	}
	used[1]=true;
	e.d=1;
	e.cnt=0;
	q.push(e);
	while(!q.empty()){
		e=q.front();
		q.pop();
		o=0;
		for(i=1;i<n+1;i++){
			if(m[e.d][i]==1&&used[i]==false){
				f.d=i;
				f.cnt=e.cnt+1;
				q.push(f);
				used[i]=true;
				t[i]=e.cnt+1;
				o=1;
			}
		}
		if(o==1){
			c++;
		}
	}
	for(i=1;i<n+1;i++){
		if(used[i]==true){
			printf("%d %d\n",i,t[i]);
		}
		else{
			printf("%d -1\n",i);
		}
	}
	return 0;
}

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

using namespace std;
static const int N = 100;
static const 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;
    int u;
    while( !q.empty()) {
        u = q.front();
        q.pop();
        for (int v = 0; v < n; v++) {
            if (M[u][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << ( (d[i] == INFTY) ? (-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++) M[i][j] = 0;
    }
    
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }
    
    bfs(0);
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;


int d[200];
bool table[200][200];

void init(){
    for(int i=0;i<200;i++){
        d[i]=-1;
        for(int j=0;j<200;j++){
            table[i][j]=false;
        }
    }
}

void bfs(int itr, int dis, int n){
    d[itr] = dis;
    for(int i=0;i<n;i++){
        if(table[itr][i]&&(d[i]<0||d[i]>dis+1)){
            bfs(i, dis+1,n);
        }
    }
}

void solve(int n){
    bfs(0,0,n);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    init();
    int n;
    cin >> n;
    int u,k,v;
    for(int i=0;i<n;i++){
        cin >> u >> k;
        for(int j=0;j<k;j++){
            cin >> v;
            table[u-1][v-1]=true;
        }
    }

    solve(n);

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

    return 0;
}

C++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>

long long int INF = 1e18;
using namespace std;

typedef pair<long long int, long long int> P;

long long int DP[200][200] = {};

int n;

int main(){
	
	cin >> n;
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			DP[i][j] = INF;
		}
		DP[i][i] = 0;
	}
	for(int loop = 0; loop < n; loop++){
		
		int id, k;
		cin >> id >> k;
		id--;
		
		for(int i = 0; i < k; i++){
			int num;
			cin >> num;
			num--;
			DP[id][num] = 1;
		}
	}
	
	for(int k = 0; k < n; k++){
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				DP[i][j] = min(DP[i][j], DP[i][k] + DP[k][j]);
			}
		}
	}
	
	for(int i = 0; i < n; i++){
		cout << i + 1 << " ";
		if(DP[0][i] == INF){
			cout << -1 << endl;
		}else{
			cout << DP[0][i] << endl;
		}
	}
	
	return 0;
}