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 <stack>
#include <queue>

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(q->size() > 0)
    {
        int u = q->front();
        q->pop();

        for (int i = 0; i < N; i++)
        {
            if (M[u][i] && C[i] == WHITE)
            {
                C[u] = GRAY;
                D[i] = D[u] + 1;
                q->push(i);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

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

    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[u][v] = 1;
        }
    }

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

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

    return 0;
}

C++: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using Graph = vector<vector<int>>;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

int N, M, K, Q, W, H, R, C;

int main() {
    // cin.tie(0);
    // ios::sync_with_stdio(false);

    cin >> N;
    Graph G(N);
    REP(i, N) {
        int u, k;
        cin >> u >> k;
        u--;
        REP(j, k) {
            int v;
            cin >> v;
            v--;
            G[i].push_back(v);
        }
    }

    vector<int> dis(N + 5, -1);
    dis[0] = 0;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int u = que.front();
        que.pop();
        for (auto v : G[u]) {
            if (dis[v] == -1) {
                que.push(v);
                dis[v] = dis[u] + 1;
            }
        }
    }
    REP(i, N) { cout << i + 1 << " " << dis[i] << endl; }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define debug(x) cout <<"DEBUG"<<" "<< #x << ":" << x << '\n'

// n回繰り返し
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順
#define rep1(i,n) for(int i = 1; i <= ((int)(n)); i++) // 1-indexed昇順
#define rrep(i,n) for(int i = ((int)(n)-1); i >= 0; i--)  // 0-indexed降順
#define rrep1(i,n) for(int i = ((int)(n)); i >= 1; i--)   // 1-indexed降順
#define all(x) (x).begin(),(x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;  // 2次元配列
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int, int> PII;

template<typename T>
using vec = vector<T>;
template<class T>
using maxheap = std::priority_queue<T>;
template<class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template<class T,class U> inline bool chmax(T &a, const U &b){if(a<b){a=b;return 1;}return 0;}
template<class T,class U> inline bool chmin(T &a, const U &b){if(a>b){a=b;return 1;}return 0;}

const ld Pi = std::acos(-1.0L);
const ll infll = (1LL<<62)-1;
const int inf = (1<<30)-1;
const int mod = 1000000007;
// const int mod = 998244353;


/*FUNCs=================================================*/


/*MAIN==================================================*/
signed main(){
    int _StartTime_ = clock();
    cin.tie(nullptr);
    ios::sync_with_stdio(false);   //cin cout 高速化
    // cout << fixed << setprecision(15);

    int n; cin >> n;
    vii g(n);
    rep(i,n) {
        int u,v,k; cin >> u >> k;
        --u;
        rep(j,k) {
            cin >> v;
            --v;
            g[u].push_back(v);
        }
    }

    vi dist(n, -1);
    queue<int> que;
    que.emplace(0);
    dist[0] = 0;

    while(que.size()) {
        int u = que.front(); que.pop();
        for(auto v : g[u]) {
            if(dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            que.emplace(v);
        }
    }

    for(int i = 0; i < n; ++i) {
        cout << i+1 << ' ' << dist[i] << '\n';
    }

    // printf("ExcutionTime: %d /ms\n",1000*(int)((clock()-_StartTime_)/CLOCKS_PER_SEC));
}

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

int main()
{
  int n;
  scanf("%d", &n);

  std::vector<std::vector<int>> graph;
  graph.resize(n);
  for (int i = 0; i < n; ++i)
  {
    int u, k, v;
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j)
    {
      scanf("%d", &v);
      graph[u - 1].push_back(v - 1);
    }
  }

  std::queue<int> vertex;
  vertex.push(0);
  int current_distance = 0;

  int distances[n];
  for (int i = 0; i < n; ++i)
    distances[i] = -1;

  while (vertex.size() > 0)
  {
    int s = vertex.size();

    // Loop for current depth
    for (int i = 0; i < s; ++i)
    {
      int id = vertex.front();
      vertex.pop();

      distances[id] = current_distance;

      std::vector<int>::const_iterator itr;
      for (itr = graph[id].begin(); itr != graph[id].end(); ++itr)
      {
        if (distances[*itr] == -1)
          vertex.push(*itr);
      }
    }

    ++current_distance;
  }

  for (int i = 0; i < n; ++i)
    printf("%d %d\n", i + 1, distances[i]);

  return 0;
}