Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

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

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

void bfs(int s) {
	int i, u, v;
	queue<int> q;
	q.push(s);
	for ( i = 0; i < n; i++ ) d[i] = INFTY;
	d[s] = 0;

	while ( !q.empty() ) {
		u = q.frout();
		q.pop();
		for ( v = 0; v < n; v++ ) {
			if ( M[u][v] == 0 ) continue;
			if ( d[v] != INFTY ) continue;
			d[v] = d[u] + 1;
			q.push(v);
			}
		}

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

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

	cin >> n;

	for ( i = 0; i < n; i++ ) {
		for ( j = 0; j < n; j++ ) M[i][j] = 0;
	}

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

	bfs(0);

	return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MAX_N = 100;
vector<int> G[MAX_N];
vector<bool> seen(MAX_N, false);

int main()
{
    int N;
    cin >> N;
    rep(i, N)
    {
        int id, u;
        cin >> id >> u;
        rep(j, u)
        {
            int target;
            cin >> target;
            target--;
            G[i].push_back(target);
        }
    }

    // 最短距離を算出
    queue<int> que;
    que.push(0);

    int distance = 0;
    int d[N];
    fill_n(d, N, -1);
    d[0] = 0;
    seen[0] = true;
    while (!que.empty())
    {
        int origin = que.front();
        int distance = d[origin];
        que.pop();
        for (int target : G[origin])
        {
            if (!seen[target])
            {
                seen[target] = true;
                d[target] = distance + 1;
                que.push(target);
            }
        }
    }

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

    return 0;
}

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

const int MAX_N = 100;
int to[MAX_N][MAX_N];
int ans[MAX_N];
bool visited[MAX_N];

int main() {
  int N;
  cin >> N;
  for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
      to[i][j] = -1;
    }
    ans[i] = -1;
    visited[i] = false;
  }
  for (int i=0; i<N; i++) {
    int tmp, n;
    cin >> tmp >> n;
    for (int j=0; j<n; j++) {
      int t;
      cin >> t; t--;
      to[i][j] = t;
    }
  }

  queue<pair<int, int> > que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    pair<int, int> tmp = que.front(); que.pop();
    int x = tmp.first, depth = tmp.second;
    if (ans[x] == -1) {
      ans[x] = depth;
    } else {
      ans[x] = min(ans[x], depth);
    }
    for (int i=0; to[x][i] != -1; i++) {
      que.push(make_pair(to[x][i], depth + 1));
    }
  }

  for (int i=0; i<N; i++) {
    cout << i+1 << " " << ans[i] << endl;
  }
  return 0;
}

C++: #include <iostream> 
#include <queue>
#define NODE_NUM 100
using namespace std;

int v_num, v_degree, v, v_id;
int adj_M[NODE_NUM][NODE_NUM], Distance[NODE_NUM], visit_state[NODE_NUM];
static const int WHITE = 0;
static const int GRAY  = 1;
static const int BLACK = 2;

void BreadthFirstSearch(int node) 
{
    queue<int> Q;
    Q.push(node);
    Distance[node] = 0;
    visit_state[node] = GRAY;

    while(! Q.empty())
    {
        int cur_node = Q.front();
        for (int i = 0; i < v_num; ++i) 
        {
            if (adj_M[cur_node][i] == 1 && visit_state[i] == WHITE) 
            {
                Q.push(i);
                visit_state[i] = GRAY;
                Distance[i] = Distance[cur_node] + 1;
            } 
        } 
        Q.pop();
        visit_state[cur_node] = BLACK;
    }
}

int main() 
{
    cin >> v_num;

    // init
    for (int i = 0; i < v_num; ++i) 
    {
        for (int j = 0; j < v_num; ++j) 
        {
            adj_M[i][j] = 0;
        }
        Distance[i] = -1;
        visit_state[i] = WHITE;
    }

    for (int i = 0; i < v_num; ++i) 
    {
        cin >> v_id >> v_degree;
        v_id--;
        for (int i = 0; i < v_degree; ++i) 
        {
            cin >> v;
            adj_M[v_id][v-1] = 1;
        }
    }

    BreadthFirstSearch(0);

    for (int i = 0; i < v_num; ++i) cout << i+1 << " " << Distance[i] << endl;
}

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();

		for (int k = 0; k < N; k++){

			if (G[cur][k] == 1 && D[k] == -1){

				D[k] = D[cur] + 1;

				Q.push(k);

			}

		}

	}

	for (int l = 0; l < N; l++){

		cerr << l + 1 << " " << D[l] << endl;

	}

}



int main(){

	scanf_s("%d", &N);

	int id, c;

	for (int i = 0; i < N; i++){

		scanf_s("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。

		scanf_s("%d", V + id);

		for (int j = 0; j < V[id]; j++){

			scanf_s("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。

			G[id][c] = 1;

		}

	}//データの読み込み完了。

	bfs(0);

}

C++: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9 + 7;
const ll LINF = 1LL << 60;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}

int n;
vector<int> g[100];

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  cin >> n;
  rep(i,n) {
    int u, k;
    cin >> u >> k;
    --u;
    rep(j,k) {
      int v;
      cin >> v;
      --v;
      g[u].push_back(v);
    }
  }
  queue<int> q;
  q.push(0);
  vector<int> dst(n, -1);
  dst[0] = 0;
  while(!q.empty()) {
    int v = q.front();
    q.pop();
    for(int u : g[v]) {
      if(dst[u] != -1) continue;
      dst[u] = dst[v]+1;
      q.push(u);
    }
  }
  rep(i,n) cout << i+1 << " " << dst[i] << '\n';
  return 0;
}