Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

func (q *Queue) push(v Pair) {
	*q = append(*q, v)
}

func (q *Queue) pop() Pair {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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';
}

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
mat g;

lli u,k,v;

queue<lli> q;
vbl vis;
lli p;
vll dp;

lli solve(lli x){
    if(x == 1) return dp[x] = 0;
    if(dp[x]) return dp[x];
    lli ret = -1;
    for(lli i = 0;i < g[x].size();i++){
        ret = solve(g[x][i]) < 0 ? ret : ret < 0 ? solve(g[x][i]) + 1 : min(ret,solve(g[x][i])+1);
    }
    return dp[x] = ret;
}

int main(){
    cin >> n;
    g = mat(n+1);
    dp = vll(n+1);
    for(lli i = 0;i < n;i++){
        cin >> u >> k;
        for(lli j = 0;j < k;j++){
            cin >> v;
            g[v].push_back(u);
        }
    }
    for(lli i = 1;i <= n;i++){
        cout << i << " " << solve(i) << endl;
    }
    return 0;
}