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>
using namespace std;
#define N 100
#define WHITE 0
#define BLACK 1

int M[N+1][N+1];
int n, dist;
int DIST[N+1];
int color[N+1];


//キューを使うプログラムを考える
void bfs(int r) {
    int u;
    int next[N+1], next_num = 0;
    bool added = false;
    //現時点での距離を入れておく
    for(int i=1; i<=n; i++) {
        if(M[r][i]==1 && color[i]==WHITE) {
            if(!added) {
                dist++;
                added = true;
            }
            color[i] = BLACK;
            DIST[i] = dist;
            next[next_num] = i;
            next_num++;
        }
    }
    
    for(int i=0; i<next_num; i++) {
        cout << "next is " << next[i] << endl;
        bfs(next[i]);
    }
}   

int main() {
    int u, k, v;

    cin >>n;
    for(int i=0; i<=n; i++) {
        DIST[i] = -1;
        color[i] = WHITE;
        for(int j=0; j<=n; j++) {
            M[i][j] = 0;
        }
    }

    for(int i=0; i<n; i++) {
        cin >> u >> k;
        for(int j=0; j<k; j++) {
            cin >> v;
            M[u][v] = 1;
        }
    }

    dist = 0;
    DIST[1] = 0;
    bfs(1);

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

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>


#define ALREADY 1
#define YET 0

int *passed;
int *distance_ps;

using namespace std;


void bfs(vector< vector<int> > a, int i, int dis){
  // i?????????vector<int>??????????????¢?????????
  dis++;
  vector<int> ai = a[i];
  int n = ai.size();
  // cout << "i: " << i << ",n: " << n << endl;
  for(int j=0;j<n;j++){
    int nexti=ai[j]-1;
    
    if(passed[nexti]==YET || distance_ps[nexti]>dis){
      passed[nexti]=ALREADY;
      distance_ps[nexti]=dis;
      bfs(a, nexti, dis);
    }
  }
}

int main(){
  int n;
  cin >> n;
  vector< vector<int> > a;
  for(int i=0;i<n;i++){
    int u;
    int k;
    cin >> u >> k;
    vector<int> tmpa;
    for(int j=0;j<k;j++){
      int tmp;
      cin >> tmp;
      tmpa.push_back(tmp);
    }
    a.push_back(tmpa);
  }
  
  int a2[n][n];
  passed = new int[n];
  distance_ps = new int[n];
  
  for(int i=0;i<n;i++){
    passed[i] = 0;
    distance_ps[i] = 1000;
  }

  distance_ps[0]=0;
  bfs(a, 0, 0);
  
  for(int i=0;i<n;i++){
    cout << i << " " << distance_ps[i] << endl;
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      a2[i][j]=0;
    }
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<(int)a[i].size();j++){
      a2[i][a[i][j]-1] = 1;
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      // if(j!=0)cout << " ";
      // cout << a2[i][j];
    }
    // cout << endl;
  }
      
  
}

C++: #include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GLAY 1

// 頂点の個数
int n;
// 隣接行列
vector<vector<int> > M(100, vector<int>(100));
// 頂点の深さを格納する配列
vector<int> D(100, -1);
// 頂点の状態を格納する配列
vector<int> color(100, WHITE);
// 訪問すべき頂点を格納するキュー
queue<int> Q;

void bfs()
{
  // 初期化
  Q.push(0);
  D.at(0) = 0;
  while (!Q.empty())
  {
    // 未訪問の頂点が残っているとき
    int u = Q.front();
    Q.pop();
    color.at(u) = GLAY;
    for (int v = 0; v < n; v++)
    {
      if (M.at(u).at(v) == 1 && color.at(v) == WHITE && D.at(v) == -1)
      {
        // vはuから直接訪問でき、かつ未訪問の頂点、かつ深さ未設定のとき
        Q.push(v);
        D.at(v) = D.at(u) + 1;
      }
    }
  }
}

int main()
{
  cin >> 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--;
      M.at(u).at(v) = 1;
    }
  }

  bfs();

  for (int i = 0; i < n; i++)
  {
    cout << i + 1 << " " << D.at(i) << endl;
  }
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(const vector<vector<int>>& graph_)
      : graph(graph_), dist(graph.size(), -1) {}

  void solve() {
    q.push(make_pair(0, 0));
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      auto v = p.first;
      auto d = p.second;
      if (dist[v] != -1 && dist[v] <= d) {
        continue;
      }
      dist[v] = d;
      for (auto& v2 : graph[v]) {
        if (dist[v2] == -1 || dist[v2] > d + 1) {
          q.push(make_pair(v2, d + 1));
        }
      }
    }
  }

  const vector<vector<int>>& graph;
  vector<int> dist;
  queue<pair<int, int>> q;
};

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

  Solver s(graph);
  s.solve();
  for (size_t i = 0; i < s.dist.size(); ++i) {
    cout << (i + 1) << " " << s.dist[i] << endl;
  }
}

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

using namespace std;
typedef long long ll;
typedef long int li;
typedef long double lb;
#define rep(i,j,n) for (ll i = j; i < (n); i++)
#define repr(i,j,n) for(ll i = j; i >= (n); i--)
#define all(x) (x).begin(),(x).end()
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<b)
#define out(ans) cout << ans << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef pair<ll,ll>P;
const ll mod=1e9+7;
const ll INF = 1LL<<60;

int main(){

  ll n;
  cin>>n;
  ll ans[n];
  vector<vector<ll> >V;
  V.resize(n);
  rep(i,0,n){
    ll a;cin>>a;
    --a;
    ll k;cin>>k;
    rep(j,0,k){
      ll b;cin>>b;
      --b;
      V[a].push_back(b);
    }
    ans[i]=INF;
  }

  queue<ll>q;
  q.push(0);

  ans[0]=0;
  while(!q.empty()){
    ll u=q.front();q.pop();

    for(auto v:V[u]){
      // out(u<<":"<<v);
      if(ans[v]!=INF)continue;
      chmin(ans[v],ans[u]+1);
      q.push(v);
    }

  }


  rep(i,0,n){
    // out(ans[i]<<"=="<<inf);
    if(ans[i]==INF)out(i+1<<" "<<-1);
    else out(i+1<<" "<<ans[i]);
  }


  return 0;
}