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<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cstdio>
#include<queue>

using namespace std;

int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    fill(D, D + N, -1);
    D[src] = 0;
    while (! Q.empty()){
        int cur = Q.front();
        Q.pop();
        cerr << cur+1 << " " << D[cur] << endl;
        for (int k = 0; k < N; k++){
            if (G[cur][k] == 1 && D[k] < D[cur]){
                D[k] = D[cur]+1;
                Q.push(k);
            }
        }
    }
}

int main(){
	scanf("%d", &N);
		int id, c;
		for (int i = 0; i < N; i++){
			scanf("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
			scanf("%d", V + id);
			for (int j = 0; j < V[id]; j++){
				scanf("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
				G[id][c] = 1;
			}
		}//データの読み込み完了。
    bfs(0);
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<algorithm>

using namespace std;

queue<int> Q;
int N;
bool PATH[101][101] = {false};
int len[101];

void BFS(int id){
    Q.pop();
    for (int i = 1; i <= N; i++) {
        if (PATH[id][i]) {
            if (len[i] > len[id] + 1) {
                len[i] = len[id] + 1;
                Q.push(i);
            }
        }
    }
}

void BFS_m(){
    while (!Q.empty()) {
        BFS(Q.front());
    }
}

int main(){
    Q.push(1);
    cin >> N;
    for (int i = 1; i <= N; i++) {
        int bb;
        int n;
        cin >> bb;
        cin >> n;
        for (int k = 0; k < n; k++) {
            int s;
            cin >> s;
            PATH[i][s] = true;
        }
    }
    for (int i = 1; i <= N; i++) len[i] = 2000000000;
    len[1] = 0;
    
    BFS_m();
    
    for (int i = 1; i <= N; i++) {
        if (len[i] == 2000000000) len[i] = -1;
        printf("%d %d\n",i,len[i]);
    }
    
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<utility>
#include<numeric>
#include<vector>
#include<map>
#include<set>
#include<tuple>
#include<stack>
#include<queue>
#include<functional>
#include<iterator>
#include<cmath>

using namespace std;

typedef pair<int,int> P;


int main(){
	int n;
	cin >> n;

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

	int d[n];
	fill(d,d+n,-1);
	queue<P> que;
	que.push(P(0,0)); //点番号（0オリジン）、距離
	d[0] = 0;
	while(!que.empty()){
		P t = que.front(); que.pop();
		for(int i=0;i<es[t.first].size();i++){
			if(d[es[t.first][i]] == -1){
				que.push(P(es[t.first][i],t.second+1));
				d[es[t.first][i]] = t.second+1;
			}
		}
	}

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

C++: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define MOD 1000000007

vector<int> g[100];

int main() {
  int n;
  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--;
      g[u].push_back(v);
    }
  }
  vector<int> d(n, 1e9);
  queue<int> que;
  que.push(0);
  d[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto u : g[v]) {
      if (d[u] == 1e9) {
        que.push(u);
        d[u] = d[v] + 1;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << (d[i] == 1e9 ? -1 : d[i]) << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
const ll mod = 1000000007;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  ll r = a%b;
  while(r != 0) {
    a = b;
    b = r;
    r = a%b;
  }
  return b;
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}

int main() {
  ll n;
  cin >> n;
  vector <vector <bool>> v(n, vector <bool> (n, 0));
  vector <ll> color(n, 0);
  vector <ll> d(n, INF);
  queue <ll> q;
  rep(i, n) {
    ll a, b, c;
    cin >> a >> b;
    rep(j, b) {
      cin >> c;
      v.at(a-1).at(c-1) = 1;
    }
  }
  color.at(0) = 1;
  d.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    ll i = q.front();
    q.pop();
    rep(j, n) {
      if (v.at(i).at(j) && color.at(j)==0) {
        color.at(j) = 1;
        d.at(j) = d.at(i) + 1;
        q.push(j);
      }
    }
    color.at(i) = 2;
  }
  rep(i, n) {
    if (d.at(i) == INF) {
      cout << i+1 << ' ' << -1 << endl;
    } else {
      cout << i+1 << ' ' << d.at(i) << endl;
    }
  }
}