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