Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: # include <bits/stdc++.h>
# define pb push_back
# define int long long
using namespace std;
void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); }
#define Cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define Cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i]
#define Cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i]
#define Cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<int>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]
void Print() { cout << endl; }
template <class Head, class... Tail> void Print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; Print(forward<Tail>(tail)...); }
template <class Type> void Print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class Type> void Print(vector<vector<Type>> &df) { for (auto& vec : df) { Print(vec); } }
void Debug() { cerr << endl; }
template <class Head, class... Tail> void Debug(Head&& head, Tail&&... tail) { cerr << head; if (sizeof...(tail) != 0) cerr << " "; Debug(forward<Tail>(tail)...); }
template <class Type> void Debug(vector<Type> &vec) { for (auto& a : vec) { cerr << a; if (&a != &vec.back()) cerr << " "; } cerr << endl; }
template <class Type> void Debug(vector<vector<Type>> &df) { for (auto& vec : df) { Debug(vec); } }
vector<int> range(int n) {
    vector<int> ar(max((int)0, n));
    iota(ar.begin(), ar.end(), 0);
    return ar;
}

signed main() {
    Cin(int, n); // 頂点数
    vector<vector<int>> G(n);
    for (auto i : range(n)) {
        Cin(int, u, k);
        u--;
        for (auto j : range(k)) {
            Cin(int, v);
            v--;
            G[u].pb(v);
        }
    }

    vector<int> dist(n, -1);
    queue<int> que;
    que.push(0);
    dist[0] = 0;
    while (!que.empty()) {
        int now = que.front(); que.pop();
        for (auto nxt : G[now]) {
            if (dist[nxt] != -1) continue;
            dist[nxt] = dist[now] + 1;
            que.push(nxt);
        }
    }

    for (auto i : range(n)) {
        cout << i+1 << " " << dist[i] << "\n";
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i< (n);i++)
using namespace std;
typedef long long ll;

struct BFS{
    vector<vector<int>> adj;
    queue<int> Q;
    vector<int> d;
    int n_;
    BFS(int n):adj(n),d(n,-1){
        n_ = n;
    }

    void init(){
        rep(i,n_){
            int u,k;cin>>u>>k;u--;
            rep(i,k){
                int tmp;cin>>tmp;tmp--;
                adj[u].push_back(tmp);
            }
        }
    }

    void visit(int u){
        Q.push(u);
        d[u] = 0;
        while(!Q.empty()){
            int v = Q.front();Q.pop();
            for(int h:adj[v]){
                if(d[h] == -1){
                    Q.push(h);
                    d[h] = d[v] + 1;
                }
            }
        }
    }
    void solve(){
        init();
        visit(0);
        rep(i,n_){
            cout<<i+1<<" "<<d[i]<<endl;
        }
    }

};

int main(){
    int n;cin>>n;
    BFS bfs(n);
    bfs.solve();
    return 0;
}

C++: #include <bits/stdc++.h>
#include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int a[101][101],start[101],end[101],n,p=0,v[101];
queue<int> q;
void bfs(int u){
	q.push(u);
	v[u]++;
	while(!q.empty()){
		u=q.front();q.pop();
		r(i,1,n+1)if(!v[i]&&a[u][i]){
			v[i]++;
			q.push(i);
			start[i]=start[u]+1;
		}
	}
}
int main(){
	int q,qq,qqq;
	cin>>n;
	r(i,0,n){
		cin>>q>>qq;
		r(i,0,qq){
			cin>>qqq;
			a[q][qqq]++;
		}
	}
	r(i,1,n+1)if(!v[i])
	bfs(i);
	r(i,1,n+1)cout<<i<<' '<<start[i]<<endl;
}

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


using namespace std;


static const  int N =  100;
static const  int no =  -1;

int graph[N][N] = {{}};
int d[N] = {};
int n;

void bfs(int v){
  queue<int> Q; 
  Q.push(v);

  int u;
    for(int i = 1;i <= n; i++){
      d[i] = no;
    
}
  d[v] = 0;

  while(!Q.empty()){
    u = Q.front();
    Q.pop();

    for(int i = 1;i <= n; i++){
      if(graph[u][i] == 0) continue;
      if(d[i] != no) continue;
      d[i] = d[u] +1;
      Q.push(i);
    }
  }
}


int main(){
  int u,v,k;
  //graph
  cin >> n ;

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


  bfs(1);

  for(int i = 1;i <= n; i++){
    cout << i << ' ' << d[i] << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main(){
  queue<int> q;
  int n,a,k,u,c[101];
  vector<int>  d[101];
  cin>>n;for(int i=0;i<n;i++){
    cin>>u>>k;c[i+1]=-1;
    d[u].resize(k);
    for(int j=0;j<k;j++) cin>>d[u][j];
  }q.push(1),c[1]=0;
  while(!q.empty()){
   a=q.front(),q.pop();
    for(int i=0;i<d[a].size();i++)
      if( d[a][i]>1 && c[d[a][i]]==-1) q.push(d[a][i]),c[d[a][i]]=c[a]+1;
  } for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

C++: #include <array>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <tuple>
#include <utility>
#include <limits>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdint>
#include <cassert>

using namespace std;

#define FOR(i, start, end) for(int i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)

int n;
int mat[100][100];
int d[100];

void solve()
{
    fill(begin(d), end(d), -1);

    queue<int> q;
    q.push(0);
    d[0] = 0;

    while(!q.empty()) {
        int i = q.front(); q.pop();
        REP(j, n) {
            if(!mat[i][j]) continue;
            if(d[j] != -1) continue;

            d[j] = d[i] + 1;
            q.push(j);
        }
    }
}

int main()
{
    cin >> n;

    REP(i, n) {
        REP(j, n) {
            mat[i][j] = 0;
        }
    }

    REP(_, n) {
        int u, k;
        cin >> u >> k;

        REP(_, k) {
            int v;
            cin >> v;
            mat[u-1][v-1] = 1;
        }
    }

    solve();

    REP(i, n) {
        cout << i+1 << " " << d[i] << "\n";
    }

    return 0;
}