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