Go: package main

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

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

C++: include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
#define int ll

int g[105][105], d[105], n;

void bfs() {
  queue<int> q;
  q.push(0);
  fill(d, d + n, -1);
  d[0] = 0; 

  while(!q.empty()) {
    int que = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (g[que][i] && d[i] == -1) {
        d[i] = d[que] + 1;
        q.push(i);
      }
    }
  }
}

signed main(void) {
  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;
      g[u-1][v-1] = 1;
    }
  }

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

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

int A[100][100],dk[100],L[100];

void BFS(int n,int u){
    int k;
    queue<int> Q;
    Q.push(u);
    dk[u]=0;L[u]=1;
    while(!Q.empty()){
        k=Q.front();Q.pop();
        for(int i=0;i<n;i++){
            if(A[k][i]==1&&L[i]==-1){
                Q.push(i);L[i]=1;dk[i]=dk[k]+1;
            }
        }
    }
}

int main(){
    int n,v,u,k;
    cin>>n;
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)A[i][j]=-1;
    for(int i=0;i<n;i++){L[i]=-1;dk[i]=-1;}

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

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

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

#define NO -1

using namespace std;


int adjacencyList[MAX + 1][MAX + 1];
int color[MAX + 1];
int d[MAX + 1];
int n;



void bfs(int o)
{
	queue<int> S;
	S.push(o);
	color[o] = GRAY;
	d[o] = 0;

	int fr;

	while (S.size() > 0) {
		fr = S.front();
		S.pop();
		color[fr] = BLACK;
		for (int i = 1; i <= n; i++) {
			if (color[i] == WHITE && adjacencyList[fr][i] != NO) {
				S.push(i);
				color[i] = GRAY;
				d[i] = d[fr] + 1;
			}
		}
	}

}

int main(void)
{

	int u;
	int k;
	int v;


	cin >> n;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			adjacencyList[i][j] = NO;
		}
	}

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

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

	}

	bfs(1);

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

		}
		else {			
			cout << -1;
		}
		cout << endl;
	}


	return 0;
}

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

using namespace std;

struct Edge{
	int to;
};

void BFS(vector<vector<Edge>>& G)
{
	queue<int> Q;
	vector<int> depth(G.size(), -1);
	int dep = 0;
	
	depth[0] = dep;
	for(auto i : G[0])
		Q.push(i.to);
	while(!Q.empty()){
		dep++;
		int size = Q.size();

		for(int i = 0; i < size; i++){
			int v = Q.front();
			Q.pop();
			if(depth[v-1] == -1){
				depth[v-1] = dep;
				for(auto i : G[v-1])
					Q.push(i.to);
			}
		}
	}

	for(int i = 0; i < G.size(); i++){
		cout << i + 1 << " " << depth[i] << endl;
	}
}

int main(void)
{
	int num;
	cin >> num;

	vector<vector<Edge>> AdjList(num);
	for(int i = 0; i < num; i++){
		int u, k;
		cin >> u >> k;
		for(int j = 0; j < k; j++){
			int v;
			cin >> v;
			AdjList[i].push_back(Edge{v});
		}
	}

	BFS(AdjList);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> e[105];
int res[105];
bool ok[105];
int n;
int inf=1<<30;
int main(){
//	freopen("a.txt","r",stdin);
	cin>>n;
	int u,v,k;
	fill(res,res+103,inf);
	while(cin>>u){
		cin>>k;
		for(int i=0;i<k;i++)cin>>v,e[u].push_back(v);
	}
	queue<pair<int,int> >q;
	q.push({1,0});
	ok[1]=1;
	while(!q.empty()){
		pair<int,int> p=q.front();q.pop();
		int i=p.first;int d=p.second;
		res[i]=min(res[i],d);
		for(int j=0;j<e[i].size();j++)
			if(!ok[e[i][j]])q.push({e[i][j],d+1}),ok[e[i][j]]=1;
	}
	for(int i=1;i<=n;i++)
		printf("%d %d\n",i,(ok[i])?res[i]:-1);
	return 0;
}

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

using namespace std;

int main(){
    int n;
    cin >> n;
    
    int g[102][102];
    int d[102];
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            g[i][j] = 0;
        }
        d[i] = -1;
    }
    
    int u,k;
    for(int i=0; i<n; i++){
        cin>>u>>k;
        for(int j=0; j<k; j++){
            int v;
            cin>>v;
            g[u][v] = 1;
        }
    }

    int q[102];
    int p = 0;
    q[++p] = 1;
    d[ q[p] ] = 0;
    while(p > 0){
        u = q[p--];
        for(int i=1; i<=n; i++){
            if(g[u][i] == 1 && d[i] == -1){
                g[u][i] = 0;
                d[i] = d[u]+1; 
                q[++p] = i;
              
            }
        }
    }
    
    for(int i=1; i<=n; i++){
        cout<< i << " " << d[i] << endl;
    }
    
    return 0;
}

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

using namespace std;

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

int n, m[N][N];
int dist[N];

void bfs(int idx)
{
	for (int i = 0; i < n; ++i) dist[i] = INFTY;

	queue<int> q;
	q.push(idx);
	dist[idx] = 0;

	while (!q.empty())
	{
		int u = q.front(); q.pop();
		for (int v = 0; v < n; ++v)
		{
			if (m[u][v] == 0) continue;
			if (dist[v] != INFTY) continue;

			dist[v] = dist[u] + 1;
			q.push(v);
		}
	}
	for (int i = 0; i < n; ++i)
		cout << i + 1 << " " << ((dist[i] == INFTY) ? (-1) : dist[i]) << endl;
}

int main()
{
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			m[i][j] = 0;
		}
	}
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int u, d;
		cin >> u >> d;
		for (int j = 0; j < d; ++j)
		{
			int v; cin >> v;
			m[u-1][v-1] = 1;
		}
	}

	bfs(0);

	return 0;
}