Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

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

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int d[128][128],solve[128];
int main(){
      int n,m,row,t;
      queue<int> que;
      cin >> n;
      for(int i= 0;i<n;i++){
            cin >>row>>m;
            for(int j = 0;j<m;j++){
                  cin>>t;
                  d[row][t] = 1;
            }
      }
      fill(solve,solve+n+2,-1);
      que.push(1);
      solve[1] = 0;
      while(!que.empty()){
            int p = que.front();
            que.pop();
            for(int i = 1;i<=n;i++){
                  if(d[p][i] && solve[i]==-1){
                        que.push(i);
                        solve[i] = solve[p] + 1;
                  }
            }
      }
      for(int i = 1;i<=n;i++)cout << i << ' '<< solve[i]<<endl;
}

C++: #include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T>
inline vector<T> make_v(const T &initvalue, size_t sz) {
    return vector<T>(sz, initvalue);
}

template <class T, class... Args>
inline auto make_v(const T &initvalue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initvalue, args...))>(sz, make_v<T>(initvalue, args...));
}

constexpr int INF = 0x3f3f3f3f;
constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
// }}} End

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    size_t N; cin >> N;

    vector<vector<size_t>> g(N);
    for (size_t i = 0; i < N; ++i) {
        size_t u, k; cin >> u >> k;
        u--;
        for (size_t j = 0; j < k; ++j) {
            size_t v; cin >> v;
            v--;
            g[u].emplace_back(v);
        }
    }

    vector<int> dist(N, INF);
    dist[0] = 0;

    queue<size_t> que;
    que.emplace(0);

    while (!que.empty()) {
        size_t now_node = que.front();
        que.pop();
        for (auto &next_node: g[now_node]) {
            if (chmin(dist[next_node], dist[now_node] + 1)) {
                que.emplace(next_node);
            }
        }
    }

    for (size_t idx = 0; idx < N; ++idx) {
        cout << idx + 1 << " " << (dist[idx] == INF ? -1 : dist[idx]) << "\n";
    }

    return 0;
}

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

int main() {
	int a[101][101] = {{0}};
	int u, k, v, km;
	km = 0;
	while ( cin >> u, k ) {
		for ( int i = 1; i <= k; i++ ) {
			cin >> v;
			a[u][v] = 1;
		}
		km++;
	}
	int n0;
	int b[101] = {- 1};
	queue<int> q;
	q.push( 1 );
	b[1] = 0;
	int cou = 0;
	for ( int i = 1; i <= km; i++ ) {
		n0 = q.front();
		q.pop();
		cou++;
		for ( int j = 1; j <= km + 1; j++ ) {
			if ( a[n0][j] == 1 && b[j] == - 1 ) {
				q.push( j );
				b[j] = cou;
			}
			if ( q.empty ) break;
		}
	}
	for ( int i = 1; i <= km; i++ )
		cout << i << " " << b[i] << endl;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); i++) 
#define FOR(i, a, b) for(int (i) = a; (i) < (int)b; i++)
#define RREP(i, n) for(int (i)=((int)(n)-1); (i)>=0; i--)
#define RFOR(i, a, b) for(int (i) =((int)(b)-1); (i)>=(int)a; i--)
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define NIL -1
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define SZ(x)  (int)x.size()
#define SP(x)  setprecision((int)x)


using namespace std ;
typedef long long ll;
using Graph = vector<vector<int>>;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<string> vstr;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} //最小公倍数

//-------------------------------------------------
// 
void yes(){
  cout <<"Yes"<<endl ;
}
void no(){
  cout <<"No"<<endl ;
}


//-------------------------------------------------
// メモ
/*






*/
//-------------------------------------------------


int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n ;
  cin >> n ;
  vint d(n,-1) ;
  vint seen(n,0) ;
  queue<int> Q ;
  vvint gra(n) ;
  for(int i=0;i<n;i++){
    int a, b;
    cin >> a >> b;
    for(int j=0;j<b;j++){
      int c ;
      cin >> c ;
      gra[a-1].PB(c-1) ;
    }
  }
  
  Q.push(0) ;
  seen[0] = 1 ;
  d[0] = 0 ;
  
  while(!Q.empty()){
    int k = Q.front() ;
    for(int i=0;i<SZ(gra[k]);i++){
      if(seen[gra[k][i]]==0){
        Q.push(gra[k][i]) ;
        seen[gra[k][i]] = 1 ;
        d[gra[k][i]] = d[k] + 1 ;
      }
    }
    Q.pop() ;
  }
  
  for(int i=0;i<n;i++){
    cout << i+1 <<" "<<d[i] <<endl ;
  }
 
  return 0 ;
}

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

int *d;
int *k;
int **a;

void bfs(){
    int i, v, u;
    queue<int> q;
    q.push(1);
    d[1] = 0;
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(i=0;i<k[u];i++){
            v = a[u][i];
            if(d[v]<0){
                q.push(v);
                d[v] = d[u] + 1;
            }
        }
    }
}

int main(){
    int i, j, n, u, v;
    scanf("%d",&n);
    d = (int *)malloc(sizeof(int)*(n+1));
    k = (int *)malloc(sizeof(int)*(n+1));
    a = (int **)malloc(sizeof(int *)*(n+1));
    for(i=0;i<=n;i++)d[i] = -1;

    for(i=0;i<n;i++){
        scanf("%d",&u);
        scanf("%d",k+u);
        a[u] = (int *)malloc(sizeof(int)*k[u]);
        for(j=0;j<k[u];j++){
            scanf("%d",&v);
            a[u][j] = v;
        }
    }

    bfs();

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

    return 0;
}