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 <vector>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef long long ll;
#define range(i,a,b) for(int i=a;i<b;++i)
#define rep(i,n) range(i,0,n)
#define INF 10000000000 //10^10
#define PI 3.141592
#define pb push_back

int main(){
    //内部処理は0indexで行う

    //入力
    int n;
    cin >> n;
    vector<vint> adj(n);
    rep(i,n){
        int u,k;
        cin >> u >> k;
        rep(j,k){
            int v;
            cin >> v;
            adj[u-1].pb(v-1);
        }
    }

    //計算
    vint d(n,-1);
    d[0]=0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int cur=Q.front(); Q.pop();
        rep(i,adj[cur].size()){
            int dst=adj[cur][i];
            if(d[dst]==-1){
                d[dst]=d[cur]+1;
                Q.push(dst);
            }
        }
    }

    //出力
    rep(i,n){
        cout << i+1 << ' ' << d[i] << endl;
    }
}

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

static const int N=100;
static const int INF=(1<<21);

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

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

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

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

using namespace std;

template<typename ISTREAM>
void readGraphList(ISTREAM& cin, vector< vector<int> >& graphList) {
	size_t n;
	cin >> n;
	graphList = vector< vector<int> >(n);
	for (size_t i = 0; i != n; ++i) {
		size_t u;
		size_t k;
		cin >> u;
		cin >> k;
		vector<int>& v = graphList.at(u - 1);
		v.resize(k);
		for (size_t j = 0; j != k; ++j) {
			cin >> v.at(j);
			v.at(j)--;
		}
	}
}
#if 1
void widthSearch(const vector<vector<int>>& graphList) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	vector<int> distanceList(n, -1);
	nodeQueue.push(0);
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		int& currentNodeDistance = distanceList.at(id);
		++currentNodeDistance;
		const vector<int>& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			int& adjDistance = distanceList.at(adjID);
			if (adjDistance != -1)continue;
			nodeQueue.push(adjID);
			adjDistance = currentNodeDistance;
		}
		nodeQueue.pop();
	}
	for (int i = 0; i != n; ++i) {
		cout << i + 1 << " " << distanceList.at(i) << endl;
	}
}
#endif

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphList(cin, graphList);
	widthSearch(graphList);
}

int main()
{
	execute(cin);

	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NIL -1
int color[MAX],d[MAX];
int N[MAX][MAX];
queue<int> Q;
int n;
void bfs(int u){
    Q.push(u);
    color[u] = GRAY;
    d[u] = 0;
    while(!Q.empty()){
        u = Q.front();
        Q.pop();
        for(int v=0;v<n;v++){
            if(N[u][v]!=0&&color[v]==WHITE){
                Q.push(v);
                d[v] = d[u]+1;
                color[v] = GRAY;
            }
        }
        color[u] = BLACK;
    }
}
void bfs_init(){
    for(int i=0;i<n;i++){
        d[i] = NIL;
    }
    for(int i=0;i<n;i++){
        color[i] = WHITE;
    }
    bfs(0);
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            N[i][j] = 0;
        }
    }
    for(int i=0;i<n;i++){
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++){
            int v;
            cin>>v;
            N[i][v-1] = 1;
        }
    }
    bfs_init();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
queue<pair<int,int> > qu;
vector<int> v[111];
int root;
int b;
int d[111];
pair<int, int> pa;
int g;
 
void wide(int num){
 
  memset(d,-1,sizeof(d));
  d[num] = 0;
  qu.push(make_pair(num,d[1]));
 
  while(!qu.empty()){
 
    pa = qu.front();
    qu.pop();
    int id = pa.first;
 
    for(int i = 0; i < (int)v[id].size(); i++){
      if(d[v[id][i]] == -1){
    d[v[id][i]] = pa.second + 1;
    qu.push(make_pair(v[id][i],d[v[id][i]]));
      }
    }
  }
  for(int i = 1; i <= g; i++){
    cout << i << " " << d[i] << endl;
  }
 
}
 
int main(){
 
  
  cin >> g;
  int n;
  for(int i = 0; i < g; i++){
    cin >> n >> b;
    for(int j = 0; j < b; j++){
      cin >> root;
      v[n].push_back(root);
    }
  }
 
  wide(1);
 
}