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;

int D[100100] = {-1};//始点からの距離を入れる
int g[1001][1001]={{0}};//要素を0で初期化
void bfs(int src,int n){//srcは開始位置この場合だと頂点1だから0
  // cerr << "bfs_root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()  ){
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting_" << cur << " " <<D[cur] <<endl;
    for(int dst= 0; dst < n; dst++){
      if(g[cur][dst]==1 && D[dst]==0){
        D[dst]=D[cur]+1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n){
  for (int i = 0; i < n; ++i)
  {
    cout << i+1 <<" "<< D[i]<<endl;
  }

}

int main(){
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n,a,b,c;
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> a >> b ;
    for (int i = 0; i < b; ++i)
    {
      cin >> c;
      g[a-1][c-1]=1;/* code */
    }
  }
  bfs(0,n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }


}

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

ll N;
vector<ll> A;
vector<vector<ll>> G;
vector<bool> used;
vector<ll> dist;

ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return b/gcd(a,b)*b;}

void bfs(vector<vector<ll>> &G) {
  queue<ll> que;
  que.push(0);
  used[0] = true;
  dist[0] = 0;

  while(que.size()) {
    ll node = que.front(); que.pop();
    for(auto next_node: G[node]) {
      if (used[next_node]) continue;
      que.push(next_node);
      dist[next_node] = dist[node]+1;
      used[next_node] = true;
    }
  }
}


int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  G.resize(N);
  used.resize(N, false);
  dist.resize(N, -1);
  rep(i, N) {
    int tmp, k;
    cin >> tmp >> k;
    rep(j, k) {
      int node;
      cin >> node;
      G[tmp-1].push_back(node-1);
    }
  }
  bfs(G);
  rep(i, N) cout << i+1 << " " << dist[i] << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    vector<vector<int>> to(n);
    rep(i, n) {
        int u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            int v;
            cin >> v;
            to[u].push_back(--v);
        }
    }

    queue<int> q;
    vector<int> dist(n, -1);
    q.push(0);
    dist[0] = 0;
    
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int i = 0; i < to[v].size(); ++i) {
            int nv = to[v][i];
            if (dist[nv] >= 0) continue;
            q.push(nv);
            dist[nv] = dist[v] + 1;
        }
    }

    rep(i, n) cout << i+1 << " " << dist[i] << endl;
}

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 <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#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<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;
}