Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

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

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, G[N][N];
int d[N];

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

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

  cin >> n;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++) G[i][j] = 0;
  }

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

  bfs(0);
  return 0;
}

C++: #include <iostream>
#include <deque>
#include <utility>
using namespace std;

int g[101][101], d[101];
deque<pair<int,int> > q;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0; i<n; i++) {
        int id, k;
        cin>>id>>k;
        g[id][0]=k;
        for(int j=1; j<=k; j++) {
            cin>>g[id][j];
        }
    }
    for(int i=0; i<=100; i++) d[i]=-1;
    q.push_back(make_pair(1,0));
    
    while(!q.empty()){
        pair<int, int> now = q.front();
        q.pop_front();
        int id=now.first, cost=now.second;
        if(d[id]!=-1) continue;
        d[id]=cost++;
        int vn=g[id][0];
        for(int i=1; i<=vn; i++) q.push_back(make_pair(g[id][i],cost));
    }
    for(int i=1; i<=n; i++) cout<<i<<' '<<d[i]<<'\n';
    return 0;
}