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++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

#define MAX 100
//#define MAX_ 1000
struct Graph
{
	int id, k;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
//int cou = 0;
void Breadth_cou(Graph *g,int cou)
{
	if (g->k >= 0) {
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			if (G[*i].d == -1)
				G[*i].d = cou;
		}
	}
	g->k = 0;
}
void Breadth(Graph *g)
{
	if (g->k >= 0) {
		Breadth_cou(g, g->d + 1);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			Breadth(&G[*i]);
		}
	}
}

int main(void)
{
	int n, u, xk, v;
	scanf_s("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf_s("%d %d", &u, &xk);
		G[u - 1].id = u - 1;
		G[u - 1].k = xk;
		for (int j = 0; j < xk; ++j) {
			scanf_s("%d", &v);
			G[u - 1].node.push_back(v - 1);
		}
	}
	G[0].d = 0;
	Breadth(&G[0]);
	for (int i = 0; i < n; ++i) {
		printf("%d %d\n", G[i].id + 1, G[i].d);
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
 
struct status{
    int idx_node;
    vector<int> children;
    int visited = 0;
    int depth = -1;
};
 
deque<int> schedule; 
vector<status> graph;

int depth = 1;
void bfs(){
    if (schedule.size()>1){
        
        int now_node = schedule.front();
        schedule.pop_front();
        
        switch(now_node){
            case 0: {
                ++depth;
                schedule.push_back(0);
                break;
            }
            default: {
                for(auto& next_node:graph[now_node-1].children){
                    if(graph[next_node-1].visited) continue;
                    
                    graph[next_node-1].visited = 1;
                    graph[next_node-1].depth = depth;  
                    schedule.push_back(next_node);
                }
            }
        }
        bfs();
    }
}
 

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //stdi
    int nb_nodes; cin >> nb_nodes;
    graph.resize(nb_nodes);
    for(auto& times:graph){
        int idx_node; cin >> idx_node; graph[idx_node-1].idx_node = idx_node;
        int nb_edges; cin >> nb_edges; graph[idx_node-1].children.resize(nb_edges);
        for(auto& a:graph[idx_node-1].children){
            cin >> a;
        }
        sort(graph[idx_node-1].children.begin(),graph[idx_node-1].children.end());
    }
    
    //bfs
    graph[1-1].depth = 0;
    schedule.push_back(1);
    schedule.push_back(0);
    bfs();
     
    //stdo
    for(auto& a:graph){
        cout<<a.idx_node<<" "
            <<a.depth<<"\n";
    }
}

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;
  vector<int> G[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--;
      G[u].push_back(v);
    }
  }
 
  queue<int> q1, q2;
  bool visited[200] = {};
  int ans[200];
 
  for(int i=0;i<n;i++) ans[i] = -1;
   
  q1.push(0);
 
  for(int i=0;q1.size();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<G[p].size();j++){
        q2.push(G[p][j]);
      }
    }
 
    q1 = q2;
    q2 = queue<int>();
  }
 
  for(int i=0;i<n;i++)
    printf("%d %d\n", i+1, ans[i]);
   
  return 0;
}

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

int main(){
  queue<int> q;
  int n,k,u,max=0;
  int g[101]={},f[101];
  int d[101][101]={};
  int di;
  cin>>n;
  for(int i=0;i<101;i++) f[i]=-1;
  f[1]=0;
  g[1]=1;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    if(max<k) max=k;
    for(int j=0;j<k;j++){
      cin>>di;
	d[u][j]=di;
    }
  }

  q.push(1);

  int a,c[101]={};
  while(!q.empty()){
 a=q.front();
    for(int i=0;i<=max;i++){

      if( d[a][i]>1 && g[d[a][i]]==0){
	g[d[a][i]]=1;
	q.push(d[a][i]);
	 c[d[a][i]]=c[a]+1;
      }
    }
      q.pop();
  }
  for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
queue<int> qu;
int v[101][101];
int dis[101], n;
int d = 0;
int tf;
inline void b1(int t) {
	for (int i = 1; i <= n; i++) {
		if (v[t][i]) {
			tf = 1;
			qu.push(i);
			dis[i] = d + 1;
			for (int j = 1; j <= n; j++)
				v[j][i] = 0;
		}
	}
}
inline void b2() {
	tf = 0;
	for (int i = 1; i <= n; i++)
		if (dis[i] == d) {
			b1(i);
			qu.pop();
		}
	d++;
}
inline void bfs() {
	tf = 1;
	b1(1);
	d++;
	qu.pop();
	while (tf) {
		b2();
	}
}
int main() {
	int u, k, s;
	cin >> n;
	for (int i = 2; i <= n; i++) {
		dis[i] = -1;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			v[i][j] = 0;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u;
		cin >> k;
		for (int j = 0; j < k; j++) {
			cin >> s;
			v[u][s] = 1;
		}
	}
	qu.push(1);
	for (int i = 1; i <= n; i++)
		v[i][1] = 0;
	bfs();
	for (int i = 1; i <= n; i++) {
		cout << i << " " << dis[i] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int n;
vector<int> a[100];
int dist[100];
const int INF = 100000000;

int main() {
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    rep(i, y) {
      int z;
      cin >> z;
      a[x-1].push_back(z-1);
    }
  }

    rep(i, n) {
      dist[i] = INF;
    }
    dist[0] = 0;

    queue<int> que;

    que.push(0);
    while(que.size()) {
      int p = que.front();
      que.pop();

      for(int i = 0; i < a[p].size(); i++) {
        if(dist[a[p][i]] > dist[p] + 1) {
          dist[a[p][i]] = dist[p] + 1;
          que.push(a[p][i]);
        }
      }
    }

  rep(i, n) {
    if(dist[i] != INF) cout << i + 1 << " " << dist[i] << endl;
    else cout << i + 1 << " " << -1 << endl;
  }
    

  return 0;
}