Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

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

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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


using namespace std;


using ll = long long;


const ll MAX = 100;


vector<list<ll>> graph;
ll d[MAX + 1] = {};
ll n;
bool visited[MAX + 1] = {};

void bfs()
{
    queue<pair<ll, ll>> q;
    q.push(make_pair(1, 0));
    visited[1] = true;

    while (!q.empty()) {
        auto tmp = q.front(); q.pop();
        ll now = tmp.first; ll dis = tmp.second;

        d[now] = dis;

        for (auto e : graph[now]) {
            if (!visited[e]) {
                visited[e] = true;
                q.push(make_pair(e, dis + 1));
            }
        }
    }
}


int main()
{
    cin >> n;
    graph.resize(n + 1);
    for (ll i = 0; i < n; i++) {
        ll u, k;
        cin >> u >> k;
        for (ll j = 0; j < k; j++) {
            ll v;
            cin >> v;
            graph[u].push_back(v);
        }
    }

    bfs();

    for (ll i = 1; i <= n; i++) {
        if (visited[i]) {
            cout << i << " " << d[i] << endl;
        } else {
            cout << i << " " << -1 << endl;
        }
    }
}

C++: #include<iostream>
#include<vector>
#include<queue>
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100
#define INF (1 << 21)
using namespace std;

int n;
int a[N+5][N+5];

vector<int> d;
vector<int> color;

void dfs(int s){
  queue<int> Q;
  
  for(int i = 0; i < n; i++) color[i] = WHITE;
  for(int i = 0; i < n; i++) d[i] = INF;

  color[s] = GRAY;
  d[s] = 0;

  Q.push(s);

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

    for(int i = 0; i < n; i++){
      if(a[u][i] && color[i] == WHITE){
	color[i] = GRAY;
	d[i] = d[u] + 1;
	Q.push(i);
      }
    }

    color[u] = BLACK;
    
  }
}

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

  d.resize(n+5);
  color.resize(n+5);

  for(int i = 0; i < n; i++){
    cin >> u >> k;
    u--;

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

  dfs(0);

  for(int i = 0; i < n; i++){
    cout << i+1 << " " << ((d[i] == INF) ? (-1):d[i]) << 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(void){
    int u,k,v;

    cin>>n;
    
    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>
using namespace std;

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

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

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

  while(!nb.empty()){
    for(vector<int>::iterator it=mat[nb.front()].begin();it!=mat[nb.front()].end;it++){
      if(d[*it]==-1){
        d[*it]=d[nb.front()]+1;
        nb.push(*it);
      }
    }
    nb.pop();
  }

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

C++: #include <stdio.h>
#include <stdlib.h>

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_N 110

int n,time,head,tail;
int queue[MAX_N];
int *dist;
int *color;
int **matrix;

void enqueue(int);
int dequeue(void);
void bfs(void);

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

  scanf("%d",&n);

  //queue = malloc(n * sizeof(int));
  color = malloc(n * sizeof(int));
  dist = malloc(n * sizeof(int));

  matrix = malloc(n * sizeof(int *));
  for(i = 0 ; i < n ; i++){
    matrix[i] = malloc(n * sizeof(int));
 }
 for(i = 0 ; i < n ; i++){
   dist[i] = -1;
 }
 for(i = 0 ; i < n ; i++){
   for(j = 0 ; j < n ; j++){
     matrix[i][j] = 0;
   }
 }

 for(i = 0 ; i < n ; i++){
    scanf("%d",&u); u--;
    scanf("%d",&k);
    for(j = 0 ; j < k ; j++){
      scanf("%d",&v); v--;
      matrix[u][v] = 1;
    }
  }

  bfs();
  for(i = 0 ; i < n ; i++){
    printf("%d %d\n",i+1,dist[i]);
  }

  for(i = 0 ; i < n ; i++){
    free(matrix[i]);
  }
  free(matrix);
  //free(queue);
  free(dist);
  free(color);
  return 0;
}

void bfs(){
  int u,i;

  head = tail = 0;
  color[0] = GRAY;
  dist[0] = 0;
  enqueue(0);

  while(head != tail){
    u = dequeue();
    for(i = 0 ; i < n ; i++){
      if(matrix[u][i] == 1 && color[i] == WHITE){
        color[i] = GRAY;
        dist[i] = dist[u] + 1;
        enqueue(i);
      }
    }
    color[u] = BLACK;
  }
}

void enqueue(int q){
  queue[tail%MAX_N] = q;
  tail++;
}

int dequeue(){
  int v = queue[head%MAX_N];
  head++;
  return v;
}

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

using namespace std;

	int TIME = 0;
	int d[101];
	int num;
	int graph[101][101] = {0};

int main(){
	int hon;
	int eda;
	int ver;
	queue<int> QUE;

	cin>>num;

	for(int i=1;i<=num;i++)
		d[i] = -1;

	for(int i=0;i<num;i++){
		cin>>eda;
		cin>>hon;
		for(int j=0;j<hon;j++){
			cin>>ver;
			graph[eda][ver]++;
		}
	}
	QUE.push(1); //start
	d[1] = 0;

	do{
		ver = QUE.front();
		QUE.pop();
		for(int i=1;i<=num;i++){
			if(graph[ver][i] == 1 && d[i]==-1){
				d[i] = d[ver] + 1;
				QUE.push(i);
			}
		}
	}while(!QUE.empty());
		
	for(int i=1;i<=num;i++){
		cout<<i<<" "<<d[i]<<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);

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;

}

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;
  }
}