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