Go: package main

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

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

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

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

    for(int i=0;i<n;i++){
        dis_min[i]=-1;
    }

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";

    //output
    for(int i=0;i<n;i++){
        std::cout<<i+1<<' '<<dis_min[i]<<std::endl;
    }

    return 0;
}

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

int main(){
	int n,m,l,i,j,p;
	int f[1000][1000];
	int D[1000] = {};
	cin >> n;
	for(i = 0; i < n; i++){
		cin >> m;
		cin >> p;
		for(j = 0; j < p; j++){
			cin >> l;
			f[m][l] = 1;
		}
	}
	queue<int> Q;
	Q.push(1);
	while(!Q.empty()){
		int cur = Q.front();
		Q.pop();
		for(i = 1; i <= n; i++){
			if(f[cur][i] == 1 && D[i] == 0){
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}				
	for(i = 1; i <= n; i++){
		if (D[i] == 0 && i !=1 )
			D[i] = -1;
		cout << i << " " << D[i] << endl;
	}
}

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