Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010


int main(){
  int n;
  bool connected[200][200] = {};
  
  scanf("%d", &n);

  for(int i=0;i<n;i++){
    int u, k, v;

    scanf("%d%d", &u, &k);
    u--;
    
    for(int j=0;j<k;j++){
      scanf("%d", &v);
      v--;

      connected[u][v] = true;
    }
  }

  queue<int> q1, q2;
  bool visited[200] = {};
  int ans[200];
  
  q1.push(0);

  for(int i=0;;i++){

    while(q1.size()){
      int p = q1.front();
      q1.pop();

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

      ans[p] = i;
      
      for(int j=0;j<n;j++){
        if(connected[p][j]){
          q2.push(j);
        }
      }
    }

    q1 = q2;
  }

  for(int i=0;i<n;i++)
    printf("%d\n", ans[i]);
  
  return 0;
}

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

using namespace std;

int main(){
  queue<int> q;
  int n,k,u;
  int c[101];
  vector<int>  d[101];
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    d[u].resize(k);
    for(int j=0;j<k;j++) cin>>d[u][j];
  }
  for(int i=0;i<=n;i++) c[i]=-1;
  q.push(1);
  c[1]=0;
  int a;
  while(!q.empty()){
    a=q.front(),q.pop();
    for(int i=0;i<d[a].size();i++)
      if( d[a][i]>1 && c[d[a][i]]==-1){
	q.push(d[a][i]);
	c[d[a][i]]=c[a]+1;
      }
  }
  for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

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


std::queue<int> q;
typedef struct{
	bool isVisited;
	int degree;
	int distance;
	int *neighbors;
} node;

void bfs(node* nodes){
	int id;
	while (!q.empty()){
		id = q.front();
		q.pop();
		for (int i=0; i<nodes[id].degree; i++){
			if (!nodes[nodes[id].neighbors[i]].isVisited){
				nodes[nodes[id].neighbors[i]].distance = nodes[id].distance+1;
				q.push(nodes[id].neighbors[i]);
				nodes[nodes[id].neighbors[i]].isVisited=true;
			}
		}
	}
}

int main(){
	int n, d, r, id;
	node *nodes;

	scanf("%d", &n);
	nodes = new node[n+1];
	for (int i=0; i<n;i++){
		nodes[i].isVisited=false;
		scanf("%d %d", &r, &d);
		nodes[i].degree=d;
		nodes[i].neighbors = new int[d];
		nodes[i].distance=-1;
		
		for (int j=0;j<d;j++){
			scanf("%d", &id);
			nodes[i].neighbors[j] = id-1;
		}
	}
	q.push(0);
	nodes[0].isVisited=true;
        nodes[0].distance=0;
	bfs(nodes);
	for (int i=0; i<n;i++){
		printf("%d %d\n", i+1, nodes[i].distance);
		delete []nodes[i].neighbors;
	}
	delete []nodes;
	return 0;
}

C++: #include<iostream>
#include<queue>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
using namespace std;
int G[100][100];
queue<int> q;

class ANS
{
public:
	int id, d;
	bool done;
};

void bfs(ANS a[])
{
	q.push(a[0].id);
	a[q.front() - 1].done = true;
	a[q.front() - 1].d++;
	while (q.empty() != true)
	{
		int Q = q.front();
		FOR(i, 1, 101)
		{
			if (G[Q-1][i-1] == 1 && a[i-1].done == false)
			{
				a[i-1].done = true;
				a[i-1].d = a[Q-1].d + 1;
				q.push(i);
			}
		}
		q.pop();
	}
}

int main()
{
	ANS ans[100];
	int n;
	cin >> n;
	FOR(i, 0, n)
	{
		ans[i] = { i + 1,-1,false };
		int u, m;
		cin >> u >> m;
		FOR(j, 0, m)
		{
			int l;
			cin >> l;
			G[u - 1][l - 1] = 1;
		}
	}
	bfs(ans);
	FOR(i, 0, n)
	{
		cout << ans[i].id << " " << ans[i].d << endl;
	}
	return 0;
}

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

using namespace std;

int D[110]={0};
void bfs(int src,int n,int g[110][110]){

queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
	int cur =Q.front();
	Q.pop();
	for(int r=0;r<n;r++){
		if (g[cur][r]==1&&D[r]==0){
			D[r]=D[cur]+1;
			Q.push(r);
		}
	}
}
	
}



int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
	cin>>t;
	g[k][t-1]=1;
}
}
bfs(0,n,g);
cout<<"1 "<<D[0]<<endl;
for(int a=1;a<n;a++){
	if(D[a]!=0){
	cout<<a+1<<" "<<D[a]<<endl;
}
else{
	cout<<a+1<<" "<<-1<<endl;
}
}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int MAX = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int NIL = -1;
int M[MAX][MAX], n;
int d[MAX], color[MAX];
queue<int> Q;

void bfs(int s)
{
    int u, v;
    Q.push(s);
    color[s] = GRAY;
    d[s] = 0;
    while (!Q.empty())
    {
        u = Q.front();
        Q.pop();
        for (int v = 0; v != n; ++v)
        {
            if (M[u][v] && color[v] == WHITE)
            {
                Q.push(v);
                color[v] = GRAY;
                d[v] = d[u] + 1;
            }
        }
        color[u] = BLACK;
    }
}

int main()
{
    int id, m, v;
    cin >> n;
    for (int i = 0; i != MAX; ++i)
    {
        d[i] = NIL;
        color[i] = WHITE;
        for (int j = 0; j != MAX; ++j)
        {
            M[i][j] = 0;
        }
    }
    for (int i = 0; i != n; ++i)
    {
        cin >> id >> m;
        for (int j = 0; j != m; ++j)
        {
            cin >> v;
            M[id-1][v-1] = 1;
        }
    }
    bfs(0);

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