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 <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[106][106];
int A[106],f[106],w,h,n,sx,sy,gx,gy,e,g[106];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=0;i<105;i++){
		for(j=0;j<105;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<105;i++){		
		A[i]=0;				
		f[i]=-1;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
	//		printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	f[1]=0;
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, g[i]);	//
	}
	return 0;
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

typedef int Weight;

struct Edge {
  int src, dst;
  Weight weight;
  Edge(int s, int d, Weight w)
    : src(s), dst(d), weight(w) {
  }
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

void addEdge(Graph &g, int a, int b, Weight w = 1) {
  g[a].emplace_back(a, b, w);
  g[b].emplace_back(b, a, w);
}

void addArc(Graph &g, int a, int b, Weight w = 1) {
  g[a].emplace_back(a, b, w);
}

void solve() {
  int n;
  cin >> n;
  Graph g(n);
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    u--;
    while (k--) {
      int v;
      cin >> v;
      v--;
      addArc(g, u, v);
    }
  }
  int c = 1;
  vi distance(n, -1);
  vi done(n);
  queue<int> q;
  distance[0] = 0;
  done[0]     = true;
  q.push(0);
  while (q.size()) {
    int x = q.front();
    q.pop();
    for (Edge e : g[x]) {
      if (done[e.dst]) continue;
      done[e.dst]     = true;
      distance[e.dst] = distance[e.src] + 1;
      q.push(e.dst);
    }
  }
  rep(i, n) {
    cout << i + 1 << ' ' << distance[i] << endl;
  }
}

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

const long N = 100;
long M[N + 1][N + 1];
long d[N + 1];
long n;
const long INFTY = 101;

long solve(long id, long dist){
	if (id == 1) d[1] = 0;
	for (long i = 2; i <= n; i++){
		if (M[id][i]){
			if (dist + 1 < d[i]){
				d[i] = dist + 1;
				solve(i, dist + 1);
			} 
		}
	}
}

int main(){
	cin >> n;
	for (long i = 1; i <= n; i++){
		d[i] = INFTY;
		for (long j = 1; j <= n; j++){
			M[i][j] = 0;
		}
	}
	for (long i = 0; i < n; i++){
		long v, k;
		cin >> v >> k;
		for (long j = 0; j < k; j++){
			long nh;
			cin >> nh;
			M[v][nh] = 1;
		}
	}
	solve(1, 0);
	for (long i = 1; i <= n; i++){
		if (d[i] == INFTY) d[i] = -1;
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

vector<int> v[101];
vector<int> dist;
queue<int> q;

int bfs() {
    dist[1] = 0;
    q.push(1);

    while (!q.empty()) {
        int c = q.front();
        q.pop();
        for (int i = 0; i < v[c].size(); i++) {
            if (dist[v[c][i]] == -1) {
                dist[v[c][i]] = dist[c] + 1;
                q.push(v[c][i]);
            }
        }
    }
}

int main() {
    int n;
    cin >> n;
    dist.resize(n + 1, -1);

    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int a;
            cin >> a;
            v[u].push_back(a);
            v[a].push_back(u);
        }
    }

    bfs();

    for (int i = 1; i <= n; i++) {
        cout << i << " " << dist[i] << endl;
    }

    return 0;
}

C++: //1933-
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define INT(x) int x; scanf("%d",&x)
#define INPUT(x) scanf("%d",&x)
#define REP1(x,n) for(int x = 0; x < n; x++)
#define REP2(x,s,e) for(int x = s; x <= e; x++)
#define BR printf("\n")
#define INF 2000000000
#define FIND 1
#define END 2

vector<int> V[100];
priority_queue<pair<int,int> > PQ;//distance,id
int d[100];
int StateNode[100] = {};

void BFS(pair<int,int> p){
    if(d[p.second] <= -p.first) {
        PQ.pop();
        if(!PQ.empty()) BFS(PQ.top());
        return;
    }
    d[p.second] = -p.first;
    PQ.pop();
    
    REP1(k,V[p.second].size()){
        if (StateNode[V[p.second][k]] == 0 || (StateNode[V[p.second][k]] == FIND && -p.first+1)) {
            PQ.push(make_pair(p.first-1,V[p.second][k]));
            StateNode[V[p.second][k]] = FIND;
        }
    }
    
    if(!PQ.empty()) BFS(PQ.top());
}

int main(){
    INT(N);
    REP1(i,N){
        int from,n;
        scanf("%d %d",&from,&n);
        REP1(j,n){
            INT(to);
            V[i].push_back(to-1);
        }
        d[i] = INF;
    }
    
    PQ.push(make_pair(0,0));
    BFS(PQ.top());
    
    REP1(i,N){
        printf("%d %d\n",i+1,d[i]);
    }
    
    return 0;
}