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++: #include<iostream>
#include<algorithm>
using namespace std;

int head, tail;
int Q[100000];
int n, d[110];
bool a[110][110] = {};

void enqueue(int x){
  
  Q[tail] = x;

  if(tail + 1 == 100000) tail = 0;
  else tail++;
}

int dequeue(){
  
  int x = Q[head];

  if(head + 1 == 100000) head = 0;
  else head++;

  return x;
}

void bfs(int u){
  
  // queue<int> Q;
  
  enqueue(u);
  d[u] = 0;
  
  while(head != tail){
    int p = Q[head];
    dequeue();
    
    for(int i = 2;i <= n;i++){
      if(a[p][i] && d[i] == 0){
        enqueue(i);
        d[i] = d[p] + 1;
      }
    }
  }
}

int main(){
  
  int u, k, v;
  
  cin >> n;
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> v;
      a[u][v] = true;
    }
  }
  
  bfs(1);
  
  for(int i = 1;i <= n;i++){
    if(d[i] || i == 1) cout << i << " " << d[i] << endl;
    else cout << i << " " << -1 << endl;
  }
  
  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;
}

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

vector<int> G[102];
int d[102];

int main()
{
    int n,u,k,v;
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> u >> k;
        for(int j = 0; j < k; ++j){
            cin >> v;
            G[u].push_back(v);
        }
    }
    
    fill(d,d + n + 1, -1);
    
    queue<int> que;
    que.push(1);
    d[1] = 0;
    while(!que.empty()){
        int p = que.front(); que.pop();
        for(vector<int>::iterator it = G[p].begin(); it != G[p].end(); ++it){
            if(d[*it] == -1){
                d[*it] = d[p] + 1;
                que.push(*it);
            }
        }
    }
    
    for(int i = 1; i <= n; ++i){
        cout << i << ' ' << d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int adj_matrix[N_MAX+1][N_MAX+1];
array<int,N_MAX+1> depth;

int n;

void bfs(queue<int>& q, const int d) {
    if (q.empty()) { return; }
    queue<int> next_q;
    while (!q.empty()) {
        int node = q.front();
        if (depth[node] < 0) {
            depth[node] = d;
            FOR(i,1,n+1) {
                if (adj_matrix[node][i] && depth[i] < 0) {
                    next_q.push(i);
                }
            }
        }
        q.pop();
    }
    bfs(next_q, d+1);
}

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

    cin >> n;

    int id, k, v;
    FOR(i,0,n) {
        cin >> id;
        cin >> k;
        FOR(j,0,k) {
            cin >> v;
            adj_matrix[id][v] = 1;
        }
    }

    depth.fill(-1);

    queue<int> q;
    q.push(1);
    bfs(q, 0);

    FOR(i,1,n+1) {
        cout << i << " " << depth[i] << endl;
    }

    return 0;
}

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

using namespace std;

int minDistance(vector<vector<int>>& edge, int st, int en) {
	size_t numNode = edge.size();
	vector<bool> passed(numNode, false);
	queue<int> v;
	queue<int> d;

	v.push(st);
	d.push(0);
	passed[st] = true;

	while(!v.empty()) {

		int nowV = v.front(); v.pop();
		int nowD = d.front(); d.pop();

		if(nowV == en) {
			return nowD;
		}

		for(auto nextV : edge[nowV]) {
			if(passed[nextV]) {
				continue;
			}

			v.push(nextV);
			d.push(nowD + 1);
			passed[nextV] = true;
		}

	}

	return -1;
}

int main() {

	int n;
	cin >> n;

	vector<vector<int>> edge(n);
	for(int i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for(int j = 0; j < k; j++) {
			cin >> v;
			edge[u - 1].push_back(v - 1);
		}
	}

	for(int i = 0; i < n; i++) {
		cout << i + 1 << " " << minDistance(edge, 0, i) << endl;
	}

	return 0;
}

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

using namespace std;

static const int MAX=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int INF=-1;
int n,adj[MAX+1][MAX+1];
int color[MAX+1],d[MAX+1];
queue<int> Q;

void bfs(){
  for(int i=1;i<=n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }
  d[1]=0;
  color[1]=GRAY;
  Q.push(1);
  while(!Q.empty()){
    int u=Q.front(); Q.pop();
    for(int i=1;i<=n;i++){
      if(adj[u][i] && color[i]==WHITE){
        color[i]=GRAY;
        d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BLACK;
  }
}

int main(){
  cin >> n;
  for(int i=1;i<=n;i++){
    int u,k; cin >> u >> k;
    for(int i=1;i<=n;i++) adj[u][i]=0;
    for(int i=0;i<k;i++){
      int v; cin >> v;
      adj[u][v]=1;
    }
  }
  bfs();
  for(int id=1;id<=n;id++)
    printf("%d %d\n",id,d[id]);
}

C++: #include<iostream>
#include<cstdio>
#define N 101
using namespace std;

typedef struct{
  int q[10000];
  int head;
  int tail;
}Queue;

Queue que;
int g[N][N]; 
int n;
int d[N];
bool us[N];
void Enque(int data){
  que.q[que.tail]=data;
  que.tail++;
}

int Deque(void){
  int data;
  data=que.q[que.head];
  que.head++;
  return data;
}

bool isEmpty(){
  if(que.tail-que.head==0)return true;
  else return false;
}


void bfs(int s){
  int u,v;
  Enque(s);
  while(true){

    if(isEmpty()==true) {
      break;
    }
    u=Deque();
    //  cout << "u = " << u << endl;
      for(int i=1;i<= n;i++){
 
      if(us[i]==false && g[u][i] == 1){
	v=i;
	//	cout << "i - " << i <<endl;
	us[i]=true;
	d[v]=d[u]+1;
	Enque(v);
      }
    }
  }
}

int main(){

  int n2,data,u;

 //load n
  cin >>n;

  //initialize
  que.tail=0;
  que.head=0;
  for(int i=0;i<N;i++){
    us[i]=false;
    d[i]=-1;
    for(int j=0;j<N;j++){
      g[i][j]=0;
    }
  }

  //load data
  for(int i=0;i<n;i++){
    scanf("%d%d",&u,&n2);
    for(int j=0;j<n2;j++){
      scanf("%d",&data);
      g[u][data]=1;
    }
  }
  //execute

  d[1]=0;
  us[1]=true;
  bfs(1);


  //print
  for(int i=1;i<=n;i++){
    printf("%d %d\n",i,d[i]); 
  }

  return 0;
}