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 <string>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <iomanip>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;

vector<int> to[101];
vector<int> d(101,-1);

int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int u, k;
        cin >> u >> k;
        for(int j = 0; j < k; j++){
            int v;
            cin >> v;
            to[u].push_back(v);
        }
    }

    queue<pair<int, int> > q;
    q.push(make_pair(1,0));

    while(q.size() > 0){
        pair<int, int> p = q.front();
        q.pop();
        int i = p.first;
        int t = p.second;

        if(d[i] != -1) continue;
        d[i] = t;
        for(int j = 0; j < to[i].size(); j++){
            q.push(make_pair(to[i][j], t+1));
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,m,k,a,b,ch[100];
vector<int>v[100];
queue<int>que;
int main(void){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>m>>k;
        for(int i=0;i<k;i++){
            cin>>a;
            v[m-1].push_back(a-1);
        }
    }
    que.push(0);
    ch[0]=1;
    while(que.size()){
        int q=que.front();
        que.pop();
        for(auto x:v[q]){
            if(ch[x]==0){
                ch[x]=ch[q]+1;
                que.push(x);
            }
        }
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<ch[i]-1<<endl;
    }
}

C++: #include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = (int)1e2 + 10;

int n;
vector<int> g[MAXN];
int vis[MAXN];
int dep[MAXN];

void BFS(){
    int d=0;
    queue<int> q;
    for(int i=1;i<=n;i++) dep[i]=-1;
    dep[1]=0;
    q.push(1);
    vis[1]=1;
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i];
            if(vis[v]) continue;
            dep[v]=dep[u]+1;
            vis[v]=1;
            q.push(v);
        }
    }
}

void work(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int u,k; scanf("%d%d",&u,&k);
        while(k--){
            int v; scanf("%d",&v);
            g[u].push_back(v);
        }
        sort(g[u].begin(),g[u].end());
    }
    BFS();
    for(int i=1;i<=n;i++) printf("%d %d\n",i,dep[i]);
}

int main(){
    work();
    return 0;
}

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

int main(void)
{
     //??£??\??????????????£??\???????????????
    int n, k;

    cin >> n;
    vector<vector<bool> > A(n, vector<bool>(n, false));
    for(int i = 0; i < n; i++){
        cin >> k >> k;
        for(int j = 0; j < k; j++){
            int input;
            cin >> input;
            A[i][input-1] = true;
        }
    }

    vector<int> u(n, 1000000);
    u[0] = 0;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(u[j] == i){
                for(int k = 0; k < n; k++){
                    if(A[j][k] == 1) u[k] = min(i+1, u[k]);
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout << i << " " << u[i] << endl;
    }
    return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
int a[101][101],num[101],dis[101];
bool vis[101];
int n,u,k,x,t=1;
queue<int> q;
int main(){
	memset(a,0,sizeof(a));
	memset(vis,false,sizeof(vis));
	memset(dis,0x1f1f1f1f,sizeof(dis));
	cin>>n;
	for (int i=0; i<n; i++){
		cin>>u>>k;
		num[u]=k;
		int j=0;
		while (k--){
			  cin>>x;
			  a[u][++j]=x;
		}
	}
	q.push(1);
	dis[1]=0;
	while (!q.empty()){
		  x=q.front(); q.pop(); vis[x]=true;
		  for (int i=1; i<=num[x]; i++){
		  	  if (!vis[a[x][i]]){
		  	     dis[a[x][i]]=min(dis[a[x][i]],dis[x]+1);
		  	     q.push(a[x][i]);
              }
		  }
	}
    for (int i=1; i<=n; i++){
    	cout<<i<<' ';
    	if (dis[i]==0x1f1f1f1f) dis[i]=-1;
		cout<<dis[i]<<endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

bool visited[111];
int d[111];
vector<vector<int> > g(111);
typedef pair<int, int> P;

int main(){
  int n; cin >> n;
  FOR(i,0,n){
    int u, k; cin >> u >> k;
    FOR(j,0,k){
      int v;
      cin >> v;
      g[u].push_back(v);
    }
  }
  FOR(i,0,n+1) visited[i] = false;
  FOR(i,0,n+1) d[i] = -1;
  queue<P> que;
  que.push(P(1,0));
  visited[1] = true;
  while(!que.empty()){
    P p = que.front();que.pop();
    int u = p.first;
    d[u] = p.second;
    for(auto &v : g[u]){
      if(!visited[v]){
        que.push(P(v, p.second + 1));
        visited[v] = true;
      }
    }
  }
  FOR(i,1,n+1){
    cout << i << " " << d[i] << endl;
  }
  return 0;
}