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

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

	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 <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

int main(){
	int n;
	cin >> n;

	set<int> A[n+1];
	int v1, v2, deg;
	r(i, n){
		cin >> v1 >> deg;

		r(j, deg){
			cin >> v2;
			A[v1].insert(v2);
		}
	}

	queue<int> s;
	s.push(1);
	int dist[n+1]{0, 1};

	while(!s.empty()){
		int v1 = s.front(); s.pop();
		for(int v2 : A[v1]){
			if(!dist[v2]){
				s.push(v2);
				dist[v2] = dist[v1]+1;
			}
		}
	}

	for(int i = 1; i <= n; i++)
		printf("%d %d\n", i, dist[i]-1);

	return 0;
}

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

class Vtx {
public:
	int id;
	int dist = -1;
	vector<int> vtxs;
};

void dfs(int n, Vtx vtxs[]){
	queue<pair<int,int> > q;
	q.push(make_pair(vtxs[0].id, 0));
	while(!q.empty()){
		pair<int,int> p=q.front();
		q.pop();
		int id=p.first-1;
		int d=p.second;
		Vtx v = vtxs[id];
		if(v.dist==-1){
			vtxs[id].dist=d;
		}
		for(int i=0;i<v.vtxs.size();i++){
			int nId=v.vtxs[i];
			if(vtxs[nId-1].dist==-1){
				q.push(make_pair(nId,d+1));
			}
		}
	}
}

int main(){
	int n;
	cin >> n;
	Vtx vtxs[n];
	for(int i=0;i<n;i++){
		int id, k;
		cin >> id >> k;
		vtxs[i].id = id;
		for(int j=0;j<k;j++){
			int v;
			cin >> v;
			vtxs[i].vtxs.push_back(v);
		}
	}
	
	dfs(n, vtxs);
	
	for(int i=0;i<n;i++){
		Vtx v = vtxs[i];
		cout << v.id << " ";
		cout << v.dist << 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>
using namespace std;

void calc(const vector<vector<int> >& G, vector<int>& D) {
    queue<int> Q1;
    D[1] = 0;
    int dim = 1;
    Q1.push(1);
    while (!Q1.empty()) {
        queue<int> Q2;
        while (!Q1.empty()) {
            int i = Q1.front();
            for (int j = 1; j < G[i].size(); j++) {
                if (G[i][j] && D[j] == -1) {
                    Q2.push(j);
                    D[j] = dim;
                }
            }
            Q1.pop();
        }
        dim++;
        Q1 = Q2;
    }

}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n + 1, vector<int>(n + 1, 0));
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            G[u][v] = 1;
        }
    }
    vector<int> D(n + 1, -1);
    calc(G, D);

    for (int i = 1; i <= n; 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 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){
    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;
    }
  }
  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<k;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<stdio.h>

int main(void) {
	int n, a[100][100] = {0}, kyori[100] = {0}, homon1[100] = {0}, homon2[100] = {0}, kyo, u, k, v, i, j, m;
	
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &u);
		scanf("%d", &k);
		for (j = 0; j < k; j++)
		{
			scanf("%d", &v);
			a[u - 1][v - 1] = 1;
		}
	}
	
	kyori[0] = 0;
	kyo = 0;
	homon1[0] = 1;
	for (m = 0; m < n; m++)
	{
		for (i = 0; i < n; i++)
		{
			if (homon1[i] == 1 && homon2[i] == 0 && kyori[i] == kyo)
			{
				for (j = 0; j < n; j++)
				{
					if (a[i][j] == 1 && homon1[j] == 0)
					{
						kyori[j] = kyori[i] + 1;
						homon1[j] = 1;
					}
				}
				homon2[i] = 1;
			}
		}
		kyo++;
	}
	
	for (i = 0; i < n; i++)
	{
		if (i != 0 && kyori[i] == 0) kyori[i] = -1;
		printf("%d %d\n", i + 1, kyori[i]);
	}
	
	return 0;
}