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 <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;



int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<vector<int> > edge(n,vector<int>());
	REP(i,n){
		int v;cin>>v;v--;int t;cin>>t;
		while(t--){
			int u;cin>>u;u--;
			edge[v].push_back(u);
		}
	}
	queue<pair<int,int> > que;
	int mindist[103];
	fill_n(mindist,n,INF);
	mindist[0] = 0;
	que.push(make_pair(0,0));
	while(!que.empty()){
		pair<int,int> v = que.front();
		que.pop();
		v.second++;
		REP(i,edge[v.first].size()){
			if(v.second < mindist[edge[v.first][i]]){
				mindist[edge[v.first][i]] = v.second;
				que.push(make_pair(edge[v.first][i],v.second));
			}
		}
	}
	REP(i,n){
		cout << i+1 << ' ';
		if(mindist[i]==INF)cout << -1 << endl;
		else	cout << mindist[i] << endl;
	}
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int main() {
    int N;
    cin >> N;
    vector< vector<int> > edges(N);
    REP(i, N) {
        int u, k;
        cin >> u >> k;
        u--;
        edges[u].resize(k);
        REP(j, k) {
            cin >> edges[i][j];
            edges[i][j]--;
        }
    }

    queue<int> que;
    vector<int> dist(N, -1);
    dist[0] = 0;
    que.push(0);
    while(!que.empty()) {
        int v = que.front();
        que.pop();

        for(int next: edges[v]) {
            if(dist[next] == -1) {
                dist[next] = dist[v] + 1;
                que.push(next);
            }
        }
    }

    REP(i, N) cout << i+1 << " " << dist[i] << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int n,M[101][101];
int d[101];

void bfs(int s){
  
  queue<int>q;
  
  q.push(s);
  
  for(int i=0;i<n;i++) d[i]=INF;
  d[s]=0;
  
  while(!q.empty()){
    
    int u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      
      if(M[u][v]==0)continue;
      if(d[v]!=INF) continue;
      d[v]=d[u]+1;
      q.push(v);
   
    }
 
  }
 
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF)?(-1):d[i] )<<endl;
  }

}

int main(){
  int u,k,v;
  cin>>n;
  
  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 <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
using P = pair<int,int>;
int n;
vector<int> graph[110];
int visited[110],dist[110];
int main()
{
    cin >>n;
    rep(i,n){
        int u;
        
        cin >> u;
        u--;
        int k;
        cin >> k;
        rep(j,k){
            int v;
            cin >> v;
            v--;
            graph[u].push_back(v);
            
        }

    }
    queue<int> q;
    
    q.push(0);
    visited[0] = 1;
    while(!q.empty()){
        auto now = q.front();q.pop();
        
        for(auto next :graph[now]){
            if(visited[next])continue;
            dist[next] = dist[now] +1;
            q.push(next);
            visited[next] = 1;
        }
    }

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

int main(){
    int n;
    cin >> n;

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

    vector<int> dist(n, -1);
    queue<int> que;

    dist[0] = 0;
    que.push(0);

    while(!que.empty()){
        int v = que.front();
        que.pop();

        for(int nv: G[v]){
            if(dist[nv] != -1) continue;

            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

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

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

using namespace std;

void bfs(vector<vector<int>>& g, vector<int>& dist) {
    queue<int> q;
    vector<bool> visited(g.size(), false);
    q.push(1);
    dist[1] = 0;
    while (!q.empty()) {
        int idx = q.front();
        q.pop();
        visited[idx] = true;

        for (int i = 0; i < g[idx].size(); ++i) {
            int adj = g[idx][i];
            if (!visited[adj]) {
                q.push(adj);
                if (dist[adj] != -1)
                    dist[adj] = min(dist[adj], dist[idx] + 1);
                else
                    dist[adj] = dist[idx] + 1;
            }
        }
    }
}

int main() {
    int n;
    cin >> n;

    vector<vector<int>> g(n + 1);
    for (int i = 1; i <= n; ++i) {
        int u, k;
        cin >> u >> k;

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

    vector<int> dist(n + 1, -1);
    bfs(g, dist);

    for (int i = 1; i <= n; ++i)
        cout << i << ' ' << dist[i] << '\n';
}