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 <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int M[N][N] = {0}, d[N], n;

void bfs() {
  int u, v;
  queue<int> q;
  for (u = 0; u < n; u++) {
    d[u] = INFTY;
  }
  d[0] = 0;
  q.push(0);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (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 (u = 0; u < n; u++) {
    cout << u + 1 << ' ' << ((d[u] == INFTY) ? (-1) : d[u]) << endl;
  }
}

int main() {
  int u, v, k, i, j;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs();

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
main(){
queue<int> q;
int n,a,k,u,c[101],i,j;
vector<int> d[101];
cin>>n;for(i=0;i<n;i++){
cin>>u>>k;c[i+1]=-1;
d[u].resize(k);
for(j=0;j<k;j++)cin>>d[u][j];
}q.push(1),c[1]=0;
while(!q.empty()){
a=q.front(),q.pop();
for(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(i=1;i<=n;i++)cout<<i<<" "<<c[i]<<endl;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;++i)

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

vector<vector<int>> node;
vector<bool> searched;
vector<int> cost;

void solve(int index){
    cost[index]=0;
    queue<int> que; que.push(index);
    REP(i,(int)node[index].size())
    while(!que.empty()){
        int order = que.front(); que.pop();
        REP(i,(int)node[order].size()){
            if(cost[node[order][i]]==-1){
                cost[node[order][i]]=cost[order]+1;
                que.push(node[order][i]);
            }
        }
    }
}

int main(){
   int n; cin>>n;
   node.resize(n);
   cost.resize(n,-1);
   REP(i,n){
    int index,size; cin >> index >> size; index--;
    node[index].resize(size);
     REP(j,size){cin >> node[index][j];node[index][j]--;}
    sort(node[index].begin(),node[index].end());
   }
   solve(0);
   REP(i,n)
    cout<<i+1<<" "<<cost[i]<<endl;
}

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

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(!q->empty())
    {
        int u = q->front();
        q->pop();

        for (int v = 0; v < N; v++)
        {
            if (M[u][v] && C[v] == WHITE)
            {
                C[v] = GRAY;
                D[v] = D[u] + 1;
                q->push(v);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

        for (int j = 0; j < N; j++)
        {
            M[i][j] = 0;
        }
    }

    for (int i = 0; i < N; i++)
    {
        int u, k;
        cin >> u >> k;
        u--;

        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

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

    return 0;
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;
#define N 100
#define INF 1e9

queue<int> Q;
int n,M[N][N];
int dist[N];

void bsf(int l){
	Q.push(l);
	for(int i = 0;i < n;i++) dist[i] = INF;

	dist[l]=0;
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		for(int i=0;i<n;i++){
			if(M[u][i] == 0)continue;
			if(dist[i] != INF)continue;
			dist[i] = dist[u] + 1;
			Q.push(i);
		}
	}

	for(int i=0;i<n;i++) cout<<i+1<<" "<<dist[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;
		}
	}
	bsf(0);
	return 0;
}

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

int main() {
    int N;
    int G[101][101];

    cin >> N;
    for (int j = 0; j <= N; ++j) for (int k = 0; k <= N; ++k) G[j][k] = 0;
    for (int _ = 0; _ < N; ++_) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; ++j) {
            int v; cin >> v;
            G[u][v] = 1;
        }
    }

    int min_dist[101];
    for (int j = 0; j <= N; ++j) min_dist[j] = -1;

    queue<int> que;
    que.push(1); min_dist[1] = 0;
    while (!que.empty()) {
        int v = que.front(); que.pop();

        for (int j = 1; j <= N; ++j) {
            if (G[v][j] && min_dist[j] == -1) {
                min_dist[j] = min_dist[v]+1;
                que.push(j);
            }
        }
    }

    for (int j = 1; j <= N; ++j) {
        cout << j << " " << min_dist[j] << endl;
    }

    return 0;
}

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

using namespace std;
const int INFTY = (1<<21);

int n;
int A[101][101];
int d[101];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  d[s] = 0;

  int u;
  while(!q.empty()) {
    u = q.front(); q.pop();
    for (int v = 0; v < n; ++v) {
      if (A[u][v] == 1 && d[v] == INFTY) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
}

int main() {
  
  cin >> n;
  
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      A[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    d[i] = INFTY;
  }

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

  bfs(0);

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