Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<deque>
#include<numeric>
#include<queue>
#include<stack>
#include<cstring>
#include<limits>
#include<functional>
#include<unordered_set>
#include<iomanip>
#include<cassert>
#include<regex>
#include<bitset>
#include<complex>
#include<chrono>
#include<random>
#define rep(i,a) for(int i=(int)0;i<(int)a;++i)
#define rrep(i,a) for(int i=(int)a-1;i>=0;--i)
#define REP(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define RREP(i,a,b) for(int i=(int)a-1;i>=b;--i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(),x.end()
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;
typedef std::vector<long long> vl;
typedef std::vector<std::vector<long long>> vvl;
#define out(x) cout<<x<<"\n";
using ll=long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

ll gcd(ll n, ll m) {
    ll tmp;
    while (m!=0) {
        tmp = n % m;
        n = m;
        m = tmp;
    }
    return n;
}

ll lcm(ll n, ll m) {
    return abs(n) / gcd(n, m)*abs(m);//gl=xy
}
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
 
using namespace std;

template<typename T>//O(V^2)　全点間距離 辺のコストがすべて等しいとき,ダイクストラより高速に使える
vector<vector<T>> bfs(vector<vector<T>> &g){
    queue<T>q;
    int n=g.size();
    T INFTY=numeric_limits<T>::max();
    vector<vector<T>>dist(n,vector<T>(n,INFTY));
    for(int i=0;i<n;++i){
        dist[i][i]=0;
        q.emplace(i);
        while(!q.empty()){
            int v=q.front();
            q.pop();
            for(auto x:g[v]){
                if(dist[i][x]==INFTY){
                    q.emplace(x);
                    dist[i][x]=dist[i][v]+1;
                }
            }
        }
    }
    return dist;
}

void solve(){
  int n;
  cin>>n;
  vector<vector<int>>g(n);
  rep(i,n){
      int id;
      cin>>id;
      id--;
      int k;
      cin>>k;
      rep(j,k){
          int a;
          cin>>a;
          a--;
          g[id].pb(a);
      }
  }
  auto d=bfs(g);
  rep(i,n){
      cout<<i+1<<" "<<(d[0][i]!=numeric_limits<int>::max()?d[0][i]:-1)<<"\n";
  }
}


int main(){
    ios::sync_with_stdio(false);
  cin.tie(0);
    cout<<fixed<<setprecision(15);
  solve();
    return 0;
}

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

void BFS(vector<vector<long long> >& Graph, long long start, vector<long long>& reach, vector<bool>& Flag) {
	queue<long long>Q;
	Q.push(start);
	Flag.at(start) = true;
	reach.at(start) = 0;
	while (!Q.empty()) {
		long long v = Q.front(); Q.pop();
		for (long long i = 0; i < Graph[v].size(); i++) {
		    long long to = Graph[v].at(i);
			if (!Flag.at(to - 1)) {
				reach.at(to - 1) = reach.at(v) + 1;
				Flag.at(to - 1) = true;
				Q.push(to - 1);
			}
		}
	}
}
int main() {
	long long N; cin >> N;
	vector<vector<long long> >Graph(N);
	for (long long i = 0; i < N; i++) {
		long long virtex, num;
		cin >> virtex >> num;
		for (long long j = 0; j < num; j++) {
			long long to; cin >> to;
			Graph.at(virtex - 1).push_back(to);
		}
	}
	vector<long long>reach(N, -1);
	vector<bool>Flag(N, false);
	BFS(Graph, 0, reach, Flag);
	for (long long i = 0; i < N; i++) {
		cout << i + 1 << ' ' << reach.at(i) << endl;
	}
}