Go: package main

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

type Queue []int

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

func (q *Queue) pop() int {
	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([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	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<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAX 100+5
int num;
int m[MAX][MAX],a[MAX],d[MAX],color[MAX];//color[i]中0--白色；1--灰色；2--黑色； 
void bfs(int v){
	queue<int> q;
	q.push(v);d[v]=0;
	color[v]=1;
	int shou;
	while(!q.empty()){
	shou=q.front();q.pop();
	for(int i=1;i<=num;i++){
		if(m[shou][i]==1&&color[i]==0){
			q.push(i);d[i]=d[shou]+1;color[i]=1;
		}
		color[shou]=2;
	}
	}
	return ;
}
int main(){
	memset(m,0,sizeof(m));
	memset(color,0,sizeof(color));
	memset(d,-1,sizeof(d)); 
	cin>>num;
	for(int i=1;i<=num;i++){
		int cnt;
		cin>>a[i]>>cnt;
		for(int j=0;j<cnt;j++){
			int c;
			cin>>c;
			m[a[i]][c]=1; 
		}
	}
	bfs(1);
	for(int i=1;i<=num;i++){
		cout<<a[i]<<" "<<d[a[i]]<<endl;
	}
	
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef long long ll;
#define range(i,a,b) for(int i=a;i<b;++i)
#define rep(i,n) range(i,0,n)
#define INF 10000000000 //10^10
#define PI 3.141592
#define pb push_back

int main(){
    //内部処理は0indexで行う

    //入力
    int n;
    cin >> n;
    vector<vint> adj(n);
    rep(i,n){
        int u,k;
        cin >> u >> k;
        rep(j,k){
            int v;
            cin >> v;
            adj[u-1].pb(v-1);
        }
    }

    //計算
    vint d(n,-1);
    d[0]=0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int cur=Q.front(); Q.pop();
        rep(i,adj[cur].size()){
            int dst=adj[cur][i];
            if(d[dst]==-1){
                d[dst]=d[cur]+1;
                Q.push(dst);
            }
        }
    }

    //出力
    rep(i,n){
        cout << i+1 << ' ' << d[i] << endl;
    }
}

C++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define FOR(i, m, n) for(ll i = m; i < (n); i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vii = vector<vi>;
using pii = pair<int, int>; 
using vl = vector<ll>;
using vll = vector<vl>;
using pll = pair<ll, ll>;

int main() {
    ll n;
    cin >> n;
    vll G(n);
    rep(i, n){
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k){
            ll a;
            cin >> a;
            a--;
            if(a < 0) continue;
            G[u].push_back(a);
        }
    }
    
    vl dist(n, -1);
    queue<ll> que;
    dist[0] = 0;
    que.push(0);
    while(!que.empty()){
        ll v = que.front();
        que.pop();

        for(auto nv : G[v]){
            if(dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    rep(i, n){
        cout  << i+1 << " " <<  dist[i] << endl;
    }
}

C++: #include <stdio.h>
#include <queue>
#include <algorithm>

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

using namespace std;

int n;
int M[N][N]; // ??£??\??????
int d[N]; // ???????????¢


void bfs()
{
    int i;
    for (i = 0; i < n; i++) {
        d[i] = -1;
    }
    
    int u;
    u = 0;
    d[u] = 0;
    queue<int> q;
    q.push(u);
    
    while (!q.empty()) {
        u = q.front();
        q.pop();
        for (i = 0; i < n; i++) {
            if (M[u][i] == 0) continue;
            if (d[i] != -1) continue;
            q.push(i);
            d[i] = d[u] + 1;
        }
    }
    
    for (i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}


int main()
{
    scanf("%d", &n);
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }
    
    // input graph
    int u, k, v;
    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        for (j = 0; j < k; j++) {
            scanf("%d", &v);
            M[u-1][v-1] = 1;
        }
    }
    
    bfs();
    
    return 0;
}

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

vector<vector<int>> g;
vector<int> d;

int main()
{
	int n;
	cin >> n;
	g = vector<vector<int>>( n );
	d = vector<int>( n );
	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--;
			g[u].push_back( v );
		}
	}

	queue<pair<int, int>> que;
	que.push( make_pair( 0, 1 ) );
	while( !que.empty() ) {
		int v, l;
		tie( v, l ) = que.front();
		que.pop();
		if( d[v] == 0 ) d[v] = l;
		for( int u : g[v] ) {
			if( d[u] ) continue;
			que.push( make_pair( u,  l + 1 ) );
		}
	}

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

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

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, int(n) + 1)
#define RREP(i, n) RFOR(i, 0, n)
#define RREP1(i, n) RFOR(i, 1, int(n) + 1)
#define ALL(c) begin(c), end(c)

int _ = (
#ifndef LOCAL
    std::cin.tie(nullptr), std::ios::sync_with_stdio(false),
#endif
    std::cout.precision(10), std::cout.setf(std::ios::fixed));

using ll = long long;
using ld = long double;
template <typename T> using vec = std::vector<T>;
using pii = std::pair<int, int>;
using namespace std;

using graph = vec<vec<int>>;

constexpr int INF = numeric_limits<int>::max() / 3;
int n;
graph adj;

void bfs() {
  vec<int> d(n, INF);
  queue<int> q;
  d[0] = 0;
  q.emplace(0);

  while (!q.empty()) {
    const auto u = q.front();
    q.pop();
    for (auto&& v: adj[u]) {
      if (d[v] == INF) {
        d[v] = d[u] + 1;
        q.emplace(v);
      }
    }
  }
  REP(i, n) {
    cout << (i + 1) << " ";
    cout << (d[i] == INF ? -1 : d[i]) << endl;
  }
}

int main() {
  int u, v, k;
  cin >> n;
  adj.resize(n);
  REP(i, n) {
    cin >> u >> k;
    adj[i].resize(k);
    REP(j, k) {
      cin >> v;
      adj[i].emplace_back(v - 1);
    }
  }
  bfs();
  return 0;
}