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;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define sortr(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define max 100005
#define nil -1

constexpr int inf = 1000000;
vector<vector<int>> g;
vector<int> d;

void bfs(int s){
    d.resize(max);
   fill(d.begin(),d.end(),inf);
   d[s]=0;
   queue<int> q;
   q.push(s);
   while(!q.empty()){
       int now=q.front();
       q.pop();
       for(auto &e:g[now]){
           if(d[e]==inf){
               d[e]=d[now]+1;
               q.push(e);
           }
       }
   }
}

int main() {
  int  n,u,k,v;
  cin>>n;
    g.resize(n);
    d.resize(n);
  rep(i,n){
      cin>>u>>k;
      u--;
      for(int j=0;j<k;j++){
          cin>>v;
          v--;
          g[u].push_back(v);
        }
    }
    bfs(0);
    rep(i,n){
        if(d[i]<inf){
            cout<<i+1<<" "<<d[i]<<endl;
        }else{
            cout<<i+1<<" "<<-1<<endl;
        }
    }
  return 0;
}

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

using namespace std;
using Graph = vector<vector<int>>;

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

	Graph G(n);

	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--;
			G[u].push_back(v);
		}
	}

	vector<int> dist(n, -1);
	queue<int> q;
	q.push(0);
	dist[0] = 0;

	while (!q.empty()) {
		int v = q.front();
		q.pop();

		for (int nv : G[v]) {
			if (dist[nv] != -1) continue;
			dist[nv] = dist[v] + 1;
			q.push(nv);
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << dist[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()) {
    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);
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int n=0;//頂点数 ○
int u=0;//頂点の番号　○
int k[101]={0};//頂点番号の値　○
int v[101][101]={0};//??
int d[101]={0};//shortest ○
int fptr=0;//デキューをするための変数　○
int lptr=0;//エンキューをするための変数　○
int u1[10000];//頂点の番号　○
int d1[10000];//頂点への最短距離　○
int id=0;//i==id.　○
void enqueue(int u2,int d2)
{
	u1[lptr]=u2;//頂点の番号はu2
	d1[lptr]=d2;//頂点への最短距離はd2
	lptr=(lptr+1)%10000;//今何番目の配列か求める
}
void bfs(void)
{
	int u3;
	int d3;

	while(fptr!=lptr)
	{
		u3=u1[fptr];//頂点番号代入
		d3=d1[fptr];//最短距離代入
		fptr=(fptr+1)%10000;
		if(d[u3]!=-1)
		{
			continue;
		}
		for(int i=0;i<k[u3];i++)
		{
			enqueue(v[u3][i],d3+1);
		}
		d[u3]=d3;
	}
	/*
	bfsの引数は二つ→それぞれをu1[fptr]、d1[fptr]に代入
	1.whileで全体を回す（条件付）
	2.デキューをする（whileの直後）
	3.最短距離が更新されてなければcontinue（条件付）	
	4.enqueue（for文で回す）
	*/
}
int main(void)
{	
	scanf("%d",&n);//頂点数を決める
	for(int i=0;i<n;i++)//0が頂点数になるまで
	{
		scanf("%d",&u);//各頂点の隣接リスト入力 1:u=%d...%d(n):u=%d
		scanf("%d",&k[u]);//配列ｋ[u]に値を入れる 1:k[1]=%d...k[u]=%d
		for(int j=0;j<k[u];j++)//j<k[u]のとき
		{
			scanf("%d",&v[u][j]);//配列ｖ[頂点の番号][]に値を入れる
		}
	}
	enqueue(1,0);
	for(int i=0;i<n+1;i++)//全ての値を未探索にする
	{
		d[i]=-1;//未探索の値
	}
	bfs();
	for(int i=1;i<=n;i++)
	{
	printf("%d %d\n",i,d[i]);
	}
	return 0;
}

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

queue<int> Q;
int n,u,k,v,tem;
int D[110];
vector<int> edges[110];
void solve() {
    D[1] = 0;
    while (! Q.empty()) {
        int cur = Q.front();
        Q.pop();
        while(edges[cur].size()){
            tem=edges[cur].back();
            edges[cur].pop_back();
            if(D[tem]==-1){
                Q.push(tem);
                D[tem]=D[cur]+1;
            }
        }
    }
        for (int i=1;i<=n;i++) {
            cout<<i<<' '<<D[i]<<endl;
        }
}


int main(){
    for(int i=0;i<110;i++){
        D[i]=-1;
    }
    cin>>n;
    for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
        cin>>v;
        edges[u].push_back(v);
    }
}
    Q.push(1);
    solve();
        return 0;
}

C++: #include "bits/stdc++.h"
#include "math.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef vector<vin> vvin;
typedef vector<pair<ll,ll> > vp;
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define drep(i,a,b) for(ll i=(a);i>=(b);--i)
#define SIZE(a) int((a).size())
const int MOD=1e9+7;

int main(){
  ll n;cin>>n;
  vvll g(n,vll(0));
  rep(i,0,n){
    ll m,k;cin>>m>>k;
    m--;
    rep(j,0,k){
      ll v;cin>>v;
      v--;
      g[m].push_back(v);
     
    }
  }
  vb seen(n,false);vll ans(n,-1);
  queue<ll> q;
  q.push(0);
  ll cnt=0;
  ans[0]=0;
  while(!q.empty()){
    ll tmp=q.front();
    q.pop();
    seen[tmp]=true;
    for(ll u:g[tmp]){
      if(seen[u])continue;
      q.push(u);
      if(ans[u]==-1)ans[u]=ans[tmp]+1;
    }
  }
  rep(i,0,n){
    cout<<i+1<<' '<<ans[i]<<endl;
  }
}

C++: #include <bits/stdc++.h>
#include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int a[101][101],start[101],end[101],n,p=0,v[101];
queue<int> q;
void bfs(int u){
	q.push(u);
	v[u]++;
	while(!q.empty()){
		u=q.front();q.pop();
		r(i,1,n+1)if(!v[i]&&a[u][i]){
			v[i]++;
			q.push(i);
			start[i]=start[u]+1;
		}
	}
}
int main(){
	int q,qq,qqq;
	cin>>n;
	r(i,0,n){
		cin>>q>>qq;
		r(i,0,qq){
			cin>>qqq;
			a[q][qqq]++;
		}
	}
	//r(i,1,n+1)if(!v[i])
	bfs(1);
	r(i,1,n+1)cout<<i<<' '<<start[i]<<endl;
}