Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;
 
class Queue{
private:
  int inf[MAX];
  int head,tail;
  
public:
  
  Queue(){
    syokika();
  }
  
  void syokika(){
    head = 0;
    tail = 0;
  }
  
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
  
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
  
  int front(){
    return inf[head];
  }
  
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
  
  int size(){
    return tail - head;
  }
  
};
 
int imap[200][200];
int k[200] = {0};
int ans[200] = {0};
bool used[200];
 
int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF; 
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }
 
   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j;
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }
 
  Queue que = Queue();
 
  que.push(u[0]);
 
  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
    que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
    used[imap[p][i]] = true;
    ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;
  
  for(int i=0;i<n;i++){
    cout << u[i]-1 << " " << ans[u[i]] << endl;
  }
 
  return 0;
}

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

//breadth first search

int main() {
	int n, u, k, v;
	vector< vector< int > > list;
	vector< int > dist;
	queue< int > que;
	
	cin >> n;
	list.resize(n);
	dist.resize(n);
	
	for(int i=0;i<n;++i){
		cin >> u >> k;
		dist[i] = -1;
		
		for(int j=0;j<k;++j){
			cin >> v;
			list[u-1].push_back(v-1);
		}
	}
	
	que.push(0);
	dist[0] = 0;
	
	while(!que.empty()){
		int cur = que.front();
		vector< int > branch = list[cur];
		que.pop();
		
		for(vector< int >::iterator it=branch.begin();it<branch.end();++it){
			if(dist[*it] == -1){
				que.push(*it);
				dist[*it] = dist[cur] + 1;
			}
		}
	}
	
	for(int i=0;i<n;++i){
		cout << i+1 << " " << dist[i] << endl;
	}
	
	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 <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int INF = 1001001001;

int main() {
    int n;
    cin >> n;
    vector< vector<int> > E(n);
    for(int i=0; i<n; i++) {
        int u,k;
        cin >> u >> k;
        E[u-1].resize(k);
        for(int j=0; j<k; j++) {
            int v;
            cin >> v;
            E[u-1][j] = v-1;
        }
    }
    int c[n];
    int d[n];
    memset(c, 0, sizeof c);
    memset(d,-1,sizeof d);

    c[0] = 1;
    d[0] = 0;
    queue<int> Q;
    Q.push(0);

    while(!(Q.empty())) {
        int u = Q.front();
        Q.pop();
        for(int v : E[u]) {
            if(c[v] == 0) {
                c[v] = 1;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        }
    }
    for(int i=0; i<n; i++) cout << i+1 << " " << d[i] << endl;
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <list>
#include <vector>

class TNode
{
	int FIndex;
	std::list<TNode*> FTarget;
public:
	TNode(int Index)
	: FIndex(Index)
	{
	}
	
	void Push(TNode* New)
	{
		FTarget.push_back(New);
	}
	
	int Distance(const TNode* Target)const
	{
		if(Target == this) return 0;
		if(std::find(FTarget.begin(), FTarget.end(), Target) != FTarget.end()) return 1;
		int Minimum = -1;
		for(std::list<TNode*>::const_iterator It = FTarget.begin(); It != FTarget.end(); ++It){
			int Distance = (*It)->Distance(Target);
			if(Distance >= 0 && (Minimum > Distance || Minimum < 0)){
				Minimum = Distance;
			}
		}
		return 1 + Minimum;
	}
};

class TGraph
{
	std::vector<TNode*> FNodes;
public:
	TGraph(int Count)
	: FNodes(Count)
	{
		for(int i = 0; i < Count; ++i){
			FNodes[i] = new TNode(i + 1);
		}
	}
	
	~TGraph()
	{
		for(unsigned int i = 0; i < FNodes.size(); ++i){
			delete FNodes[i];
		}
	}
	
	void InputNode()
	{
		int Index;
		std::cin >> Index;
		int BranchCount;
		std::cin >> BranchCount;
		for(int i = 0; i < BranchCount; ++i){
			int Target;
			std::cin >> Target;
			FNodes[Index - 1]->Push(FNodes[Target - 1]);
		}
	}
	
	int MeasureDistance(int Start, int End)const
	{
		return FNodes[Start - 1]->Distance(FNodes[End - 1]);
	}
};

int main()
{
	int Count;
	std::cin >> Count;
	TGraph Graph(Count);
	for(int i = 0; i < Count; ++i){
		Graph.InputNode();
	}

	for(int i = 0; i < Count; ++i){
		std::cout << i + 1 << " " << Graph.MeasureDistance(1, i + 1) << std::endl;
	}
	return 0;
}