Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

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

using namespace std;

int n;
int vertex[101];
int graph[101][101];
int dis[101];

queue<int> Q;

int main() {
	cin >> n;
	int k,d;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			graph[i][j] = -1;
		}
		dis[i] = -1;
	}


	for (int i = 1; i <= n; ++i) {
		cin >> vertex[i] >> k;
		for (int j = 0; j < k; ++j) {
			cin >> d;
			graph[i][d] = 1;
		}
	}

	Q.push(1);
	dis[1] = 0;

	while (!Q.empty()) {
		k = Q.front();
		Q.pop();
		for (int i = 1; i <= n; ++i) {
			if (graph[k][i] == 1 && dis[i] == -1) {
				Q.push(i);
				dis[i] = dis[k]+1;
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		cout << i << ' ' << dis[i] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
vi M(MAX, -1);
int main() {
	int n; cin >> n;
	M[1] = 0;
	deque<pair<int, int>> dq;
	int u, k, v;
	rep(i, 0, n) {
		cin >> u >> k;
		rep(j, 0, k) {
			cin >> v;
			if(u == 1) M[v] = 1;
			else dq.eb(mp(u, v));
		}
	}
	while(!dq.empty()) {
		tie(u, v) = dq.front(); dq.pop_front();
		if(M[u] == -1) dq.eb(mp(u, v));
		else if(M[v] == -1 || M[v] > M[u] + 1) M[v] = M[u] + 1;
	}
	rep(i, 1, n + 1) {
		cout << i << " " << M[i] << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<queue>
#define MAX 100
#define INFTY -1

using namespace std;
queue<int> Q;
int d[MAX];
int A[MAX][MAX]={0};
int n;

void bfs(int u)
{
	Q.push(u);
	d[u] = 0;

	while(!Q.empty())
	{
		int v = Q.front();
		Q.pop();
		for(int i=0; i<n; i++)
		{
			if(A[v][i] == 1)
			{
				if(d[i] == INFTY)
				{
					Q.push(i);
					d[i] = d[v] + 1;
					//printf("%d %d\n",v+1,d[v]);
				}
			}
		}
	}
}

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

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

	return 0;
}

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

int n,u,k,v;
int g[102][102];
int d[102];
int sn[102];
int cnt=0;
int x,siz;

queue<int>q;

int main(){
  cin>>n;
  memset(g,0,sizeof(g));
  for(int i=1;i<=n;i++){
    cin>>u>>k;
    d[i]=-1;
    sn[i]=0;
    for(int j=1;j<=k;j++){
      cin>>v;
      g[u][v]=1;
    }
  }
  
  q.push(1);

  while(!q.empty()){

    siz=q.size();
    cout<<siz<<endl;

    for(int i=0;i<siz;i++){
      
      x=q.front();
      q.pop();
      
      sn[x]=1;
      if(d[x]==-1)d[x]=cnt;
      
      for(int j=1;j<=n;j++){
	
	if(g[x][j]==1 && sn[j]==0){
	  q.push(j);
	}
      
      }

    }

    cnt++;

  }

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

  }

}

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

using namespace std;

#define MAX 100

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

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(!q->empty())
    {
        int u = q->front();
        q->pop();

        for (int v = 0; v < N; v++)
        {
            if (M[u][v] && C[v] == WHITE)
            {
                C[v] = GRAY;
                D[v] = D[u] + 1;
                q->push(v);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

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

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

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

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

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

    return 0;
}

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


int main() {
	int k[110][110];
int n,l,a,b;
cin>>n;
for(int j=0;j<n;j++){
	cin>>l>>a;
	for(int i=0;i<a;i++){
		cin>>b;
		k[j][b-1]=1;
	}
}
int q[105];
for(int j=0;j<n;j++){
	q[j]=-1;
	if(k[0][j]==1){
		q[j]=1;
	}
}
q[0]=0;
int u=1;
int m=1;
while(m<=n){

	for(int d=0;d<n;d++){
		if(q[d]==m){
			for(int h=0;h<n;h++){
				if(k[d][h]==1 &&q[h]==-1){
					q[h]=m+1;
				}
			}
		}
	}
m=m+1;	
}
for(int y=0;y<n;y++){
	cout<<y+1<<" "<<q[y]<<endl;
}

	return 0;
}

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


int n;

string *color = new string[n];

int *d = new int[n];

queue<int> q;

int c[100][100];

void bfs(){
  color[0] = "gray";
  d[0] = 0;

  q.push(0);


  while(q.size() != 0){
    int u = q.front();
    q.pop();

    for(int v = 0; v < n; v++){
      if(c[u][v] == 1 && color[v] == "white"){
        color[v] = "gray";
        d[v] = d[u] + 1;
        q.push(v);
      }

    }
    color[u] = "black";
  }
}

int main(){

  cin >> n;

  color = new string[n];

  d = new int[n];


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

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

  for(int i = 0; i < n; i++){
    color[i] = "white";
  }

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

  bfs();





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




  return 0;
}