Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

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

C++: // #Sazaの1日1AC
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000000 + 7;
const long long INF = 9e18;
const double PI = 3.14159265358979323846;

//マクロ
//型エイリアス
using ll = long long;
using P = pair<long long, long long>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vP = vector<pair<long long, long long>>;
//ショートカット
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, k, n) for (int i = k; i < n; i++) //半開区間
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define mkp make_pair
//入力
#define vin(v, N) for (long long i = 0; i < N; i++) cin >> v.at(i)
#define lin(n)long long n;cin >> n
#define chin(x)char x;cin >> x;
#define sin(s)string s;cin >> s;
#define vlin(v, N)vector<long long> v(N);for (long long i = 0; i < N; i++)cin >> v.at(i)
#define max(a, b) max((ll)a, (ll)b)
#define min(a, b) min((ll)a, (ll)b)

//関数
//最大公約数
long long gcd(long long m, long long n){
  long long a = max(m, n);
  long long b = min(m, n);
  long long r = a % b;
  while (r != 0){
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
// 最小公倍数
long long lcd(long long m, long long n){
  return m * n / gcd(m, n);
}
//xのn乗
long long power(long long x, long long N){
  long long ret = 1;
  for (long long i = 0; i < N; i++)
    ret *= x;
  return ret;
}
//繰り返し二乗法
long long repeat_squaring(long long x, long long n){
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
  {
    long long t = repeat_squaring(x, n / 2);
    return t * t % mod;
  }
  else
    return x * repeat_squaring(x, n - 1);
}
//素因数分解(mapで返す)
map<long long, long long> factor(long long p){
  ll p1 = p;
  map<long long, long long> ret;
  for (long long i = 2; i * i <= p1; i++)
  {
    while (p % i == 0)
    {
      ret[i]++;
      p /= i;
    }
  }
  if (p != 1)
    ret[p]++;
  return ret;
}
//素数判定
bool is_prime(long long N){
  for (long long i = 2; i * i <= N; i++)
  {
    if (N % i == 0)
      return false;
  }
  return true;
}
//最大値更新
void chmax(long long &a, long long b){
  a = max(a, b);
}
//最小値更新
void chmin(long long &a, long long b){
  a = min(a, b);
}

//構造体
//Union-Find木
struct UnionFind
{
  //メンバ変数
  vector<long long> par; //par[i]:=頂点iの親
  vector<long long> s;   //s[i]:=頂点iが属する集合の個数(iが根のとき)

  //コンストラクタ
  UnionFind(long long N) : par(N), s(N){
    for (long long i = 0; i < N; i++)
    {
      par[i] = i; //最初はすべてが根として初期化
      s[i] = 1;
    }
  }
  //メンバ関数
  //root(i):=頂点iの根
  long long root(long long i){
    if (par[i] == i)
      return i;
    par[i] = root(par[i]); //経路圧縮
    return root(par[i]);   //再帰
  }
  //size[i]:=iが属する集合の個数
  long long size(long long i){
    return s[root(i)];
  }
  //same(x,y) xとyが同じ根を持つか(同じ集合に含まれるか)
  bool same(long long x, long long y){
    return root(x) == root(y);
  }
  //unite(x,y) xの根をyの根に繋げる(集合を合併)
  void unite(long long x, long long y){
    if (!same(x, y)){
      long long rx = root(x);
      long long ry = root(y);
      par[rx] = par[ry];
      s[ry] = s[rx] + s[ry];
    }
  }
};
//Binary Indexed Tree
struct BIT{
  vector<long long> bit;
  long long N;

  BIT(long long n){
    N = n;
    bit.resize(N + 1);
  }

  void add(long long i, long long w){
    for (long long x = i; x <= N; x += x & -x){
      bit[x] += w;
    }
  }
  long long sum(long long i){
    long long ret = 0;
    for (long long x = i; x > 0; x -= x & -x){
      ret += bit[x];
    }
    return ret;
  }
};
ll dh[] = {1,0,-1,0};
ll dw[] = {0,1,0,-1};
ll H,W;
/////////////////⊂('ω'⊂ )))Σ≡GO!/////////////////
ll dfs(vvl &c,ll h, ll w){
  if(h < 0 || H <= h || w < 0 || W <= w || c[h][w] == 0) return 0;
  c[h][w] = 0;
  ll ret = 0;
  rep(i,4){
    ll nh = h + dh[i], nw = w + dw[i];
    chmax(ret, dfs(c, nh, nw) + 1);
  }
  c[h][w] = 1;

  return ret;
}

//メイン関数

int main(){
  //doubleの桁数
  cout << fixed << setprecision(10);

  lin(N);
  vvl G(N);
  rep(i,N){
    lin(id);id--;lin(k);
    G[id].resize(k);
    rep(i,k){
      cin>>G[id][i];G[id][i]--;
    }
  }

  vl d(N, -1);
  d[0] = 0;
  queue<ll> Q;
  Q.push(0);
  while(Q.size() > 0){
    ll u = Q.front(); Q.pop();
    rep(i, G[u].size()){
      ll v = G[u][i];
      if(d[v] != -1) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }

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

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

int n, dd[101], f[101], tmap[101][101];
int color[101], t = 0;
queue<int> Q;


void a(){
	while (!Q.empty()){
		int d = Q.front();
		Q.pop();
		for (int i = 1; i <= n; i++){
			if (color[i] == 0 && tmap[d][i] == 1){
				Q.push(i);
				color[i] = 1;
				dd[i] = dd[d] + 1;
			}
		}
	}
}


int main(){
	cin >> n;
	for (int i = 0; i < 100; i++){
		dd[i] = f[i] = color[i] = 0;
		for (int j = 0; j < 100; j++){
			tmap[i][j] = 0;
		}
	}

	int u, k, v;

	for (int i = 0; i < n; i++){
		cin >> u >> k;
		for (int j = 0; j < k; j++){
			cin >> v;
			tmap[u][v] = 1;
		}
	}
	for (int i = 1; i <= n; i++){
		if (color[i] == 0){
			Q.push(i);
			color[i] = 1;
			dd[i] = t;
			a();
		}
	}
	for (int i = 1; i <= n; i++){
		cout << i << " " << dd[i];
	}
	return 0;
}

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 = 0;
};
 
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
    schedule.push_back(1);
    schedule.push_back(0);
    bfs();
     
    //stdo
    for(auto& a:graph){
        cout<<a.idx_node<<" "
            <<a.depth<<"\n";
    }
}