Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

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

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

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


using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

void bfs(int s) {
    queue<int> q;
    q.push(s);
    for ( int i = 0; i < n; i++) d[i] = INFTY;
    d[s] = 0;
    int u;
    while ( !q.empty() ) {
        u = q.front(); q.pop();
        for (int v = 0; v < n; v++) {
            if (M[u][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for ( int i = 0; i < n; i++) {
        cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
    }
}


int main() {
    int u, k, v;

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

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

    bfs (0);

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
//#define ll long long
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define all(x) (x).begin(),(x).end()
ll tmp1,tmp2,tmp3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;
void swap(ll *x,ll *y){
  ll tmp;
  tmp = *x;
  *x = *y;
  *y = tmp;
}
//vector<ll> visited(100,0);
vector<ll> vec[100];
vector<ll> cou(100,-1);
ll sum = 0;
queue<pair<ll,ll>> q;
void bfs(ll k,ll num){
  q.push(make_pair(k,num));
  pair<ll,ll> piv;
  while(!q.empty()){
    piv = q.front();
    q.pop();
    if(cou[piv.first] != -1){
      continue;
    }
    cou[piv.first] = piv.second;
    for(auto itr:vec[piv.first]){
      q.push(make_pair(itr,piv.second+1));
    }
  }
  cou[k] = num;

}
int main(){
  ll n;
  cin >> n;
  ll u,k,v;
  rep(i,n){
    cin >> u >> k;
    u--;
    rep(j,k){
      cin >> v;
      v--;
      vec[u].push_back(v);
    }
  }
  bfs(0,0);
  rep(i,n){
    cout << i+1 << " " << cou[i] << endl;
  }
}

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

using Graph = std::vector<std::vector<uint32_t>>;

int main() {
  uint32_t n = 0;
  std::cin >> n;
  Graph G(n + 1);
  for (size_t i = 1; i < n + 1; i++) {
    uint32_t k = 0;
    std::cin >> i >> k;
    if (k > 0) G[i] = std::vector<uint32_t>(k);
    for (auto&& g : G[i]) std::cin >> g;
  }

  std::vector<int32_t> dist(n + 1, -1);
  dist[1] = 0;
  std::queue<uint32_t> Q({1});
  while (!Q.empty()) {
    uint32_t node = Q.front();
    Q.pop();
    for (auto&& to : G[node]) {
      if (dist[to] != -1) continue;
      Q.push(to);
      dist[to] = dist[node] + 1;
    }
  }

  for (size_t i = 1; i < dist.size(); i++) {
    std::cout << i << " " << dist[i] << std::endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int n;
int k;
int m;

int main() {
  cin >> n;
  int V[n];

  // int型の2次元配列(3×4要素の)の宣言
  vector<vector<int>> A(n, vector<int>(n));

  for (int i=0; i<n; i++) {
    cin >> V[i];
    cin >> k;
    if (k==0) {
      continue;
    } else {
      for (int j =0; j<k; j++) {
        cin >> m;
        A[i][m-1] = 1;
      }
    }
  }

  int D[n];
  for (int i=0; i<n; i++) D[i] = -1;
  D[0] = 0;

  queue<int> Q;
  Q.push(0);
  while(! Q.empty()) {
    int cur = Q.front();
    Q.pop();

    for (int i=0; i<n; i++) {
      if (A[cur][i] == 1 && D[i] == -1) {
        D[i] = D[cur] + 1;
        Q.push(i);
      }
    }
  }

  for (int i=0; i<n; i++) {
    cout << i+1 << " " << D[i] << endl;
  }
}

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


class Graph {
  public:
    Graph(int n) : n(n) {
        adj_arr = std::vector<std::vector<int>>(n);
        depth = std::vector<int>(n, -1);
    }

    void breadth_search(int i, int d) {
        std::queue<int> queue;
        depth[i] = d;
        
        for (auto && v : adj_arr[i]) {
            if (depth[v - 1] == -1 || depth[v - 1] > d) {
                queue.push(v - 1);
            }
        }

        while (!queue.empty()) {
            int v = queue.front();
            queue.pop();
            breadth_search(v, d + 1);
        }
    }

    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i + 1 << " " << depth[i] << std::endl;
        }
    }

  public:
    std::vector<std::vector<int>> adj_arr;
    std::vector<int> depth;
    
  private:
    int n;
};


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

    Graph graph(n);

    for (int i = 0; i < n; i++) {
        int u, k;
        std::cin >> u >> k;
        std::vector<int> arr(k);
        
        for (int j = 0; j < k; j++) {
            std::cin >> arr[j];
        }
        graph.adj_arr[u - 1] = arr;
    }

    graph.breadth_search(0, 0);
    graph.print();
}

C++: #include <bits/stdc++.h>
using namespace std;

vector<vector<int>> v;
vector<int> d, t;

void bfs(){
  int time = 0;
  queue<int> q;
  q.push(0);

  while(!q.empty()){
    int node = q.front();
    q.pop();

    for(int i=0; i<v.size(); ++i){
      if(v[node][i] == 1 && t[i] == -1){
         q.push(i);
         t[i] = t[node] + 1;
       }
    }
  }
}

int main(){
  int n; cin >> n;

  v.resize(n);
  d.resize(n);
  t.resize(n);
  for(int i=1; i<n; ++i) t[i] = -1;

  for(int i=0; i<n; ++i) v[i].resize(n);

  for(int i=0; i<n; ++i){
    int u, k; cin >> u >> k;
    while(k--){
      int x; cin >> x;
      v[u-1][x-1] = 1;
    }
  }

  bfs();

  for(int i=0; i<n; ++i){
      cout << i+1 << " " << t[i] << endl;
  }
}