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;
#define INFTY (1<<30)

int n;
vector<vector<int>> M(101,vector<int>(101,0));
vector<int> color(101,0),d(101,INFTY);
queue<int> Q;

void bfs(){
    color[1]=1;
    d[1]=0;
    Q.push(1);
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        for(int v=1;v<n+1;v++){
            if(M[u][v]&&color[v]==0){
                color[v]=1;
                d[v]=d[u]+1;
                Q.push(v);
            }
        }
        color[u]=2;
    }
}


int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        int a,b,k;
        cin>>a>>k;
        for(int j=0;j<k;j++){
            cin>>b;
            M[a][b]=1;
        }
    }

    bfs();

    for(int i=1;i<n+1;i++){
        cout<<i<<' ';
        if(d[i]==INFTY) cout<<-1<<endl;
        else cout<<d[i]<<endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<queue>

using namespace std;
using ll = long long;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int n; cin >> n;
  vector<vector<int>> 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 now = q.front();
    q.pop();
    for(int nx: g[now]){
      if(dist[nx] == -1){
        dist[nx] = dist[now] + 1;
        q.push(nx);
      }
    }
  }
  for(int i = 0; i < n; i++) cout << i+1 << ' ' << dist[i] << endl;
  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}

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

void InputSet(std::vector<int> & a) {
	int n;
	cin >> n;
	a.resize(n,0);
	for(int i=0;i<n;++i) {
		cin >> a.at(i);
		a.at(i)--;
	}
}
void BFS(vector<vector<int> > & Ad,
		 vector<int> & V,
		 queue<int> & Q
		 )
{
	while(!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for(int i=0;i<(int)Ad[u].size();++i) {
			int next = Ad[u][i];
			if (V[next] < 0) {
				V[next] = V[u] + 1;
				Q.push(next);
			}
		}
	}
}
int main(){
	int n;
	cin >> n;
	vector<vector<int> > Ad(n);
	vector<int> V(n,-1);
	queue<int> Q;
	for(int k=0;k<n;++k) {
		int u;
		cin >> u;
		--u;
		InputSet(Ad[u]);
	}
	Q.push(0);
	V[0]=0;
	BFS(Ad,V,Q);

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

C++: //
#if 0

#endif // 0
#include<iostream>
#include<queue>
#define MAX 100
using namespace std;
int matri[MAX][MAX];

int main()
{
    int n;
    int r[MAX];
    queue<int> q;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        r[i]=0;
        for(int j=0;j<n;j++)
            matri[i][j]=0;
    }
    for(int i=0;i<n;i++)
    {
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            int m;
            cin>>m;
            matri[u-1][m-1]=1;
        }
    }
    q.push(0);
    r[0]=0;
    while(!q.empty())
    {
        int t;
        t=q.front();
        q.pop();
        for(int i=0;i<n;i++)
            if(matri[t][i]&&r[i]==0&&i)
            {
                q.push(i);
                r[i]=r[t]+1;
               // cout<<r[i]<<endl;
            }
    }
    for(int i=0;i<n;i++)
    {
        if(r[i]||r[i]==i)
            cout<<i+1<<" "<<r[i]<<endl;
        else
            cout<<i+1<<" -1"<<endl;
    }
    return 0;
}

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]++;
		}
	}
	bfs(1);
	r(i,1,n+1){
		if(i!=1&&start[i]==0)cout<<i<<' '<<-1<<endl;
		else cout<<i<<' '<<start[i]<<endl;
	}
}

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"
*/

const int WHITE=0, GRAY=1, BLACK=2;
const int INF = 10000;
/*
void bfs(int u, int d[], int color[], int N, int M[][]){
    d[u] = 0;
    color[u] = GRAY;
    queue<int> Q;
    Q.push(u);
    while(!Q.empty()){
        int current_u = Q.front(); Q.pop();
        rep(i,N){
            if(M[current_u][i] && color[i] == WHITE){
                Q.push(i); d[i] = d[u]+1;
            }
        }
    }
    rep(i,N){
        printf("%d %d\n", i+1, d[i]);
    }
}
*/

int main()
{
    int N; cin >> N;
    int d[N], M[N][N], color[N];
    // 初期化
    rep(i, N){
        d[i] = INF;
        color[i] = WHITE;
        rep(j,N) M[i][j] = false;
    }
    // 辺の情報を読み込む
    rep(i,N){
        int id; cin >> id;
        int k; cin >> k;
        rep(j,k){
            int vj; cin >> vj;
            M[i][vj-1] = true;
        }
    }
    d[0] = 0;
    color[0] = GRAY;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int current_u = Q.front(); Q.pop();
        // cout << current_u << endl;
        rep(i,N){
            if(M[current_u][i] && color[i] == WHITE){
                Q.push(i); d[i] = d[current_u]+1;
                color[i] = GRAY;
            }
        }
        color[current_u] = BLACK;
    }
    rep(i,N){
        if(d[i] == INF) printf("%d %d\n", i+1, -1);
        else printf("%d %d\n", i+1, d[i]);
    }
}

C++: #include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
int n, visited[101], d[101];
int adj[101][101];
queue<int> q;
void bfs(int u)
{
  if(visited[u]) return;
  q.push(u);
  visited[u] = 1;
  while(!q.empty())
  {
    int v = q.front();
    for(int i=1; i<=n; i++)
    {
      if(adj[v][i] && visited[i]==0)
      {
        q.push(i);
        visited[i] = 1;
        d[i] = d[v] + 1;
      }
    }
    q.pop();
  }
}
int main()
{
  scanf("%d", &n);
  for(int i=0; i<=n; i++)
  {
    for(int j=0; j<=n; j++)
      adj[i][j] = 0;
    visited[i] = 0;
    d[i] = -1;
  }
  d[1] = 0;
  int u, k, v;
  for(int i=0; i<n; i++)
  {
    scanf("%d%d", &u, &k);
    for(int j=0; j<k; j++)
    {
      scanf("%d", &v);
      adj[u][v] = 1;
    }
  }
//  for(int i=1; i<=n; i++)
//  {
//    if(visited[i] == 0)
//      bfs(i);
//  }
  bfs(1);
  for(int i=1; i<=n; i++)
  {
    printf("%d %d\n", i, d[i]);
  }


  return 0;
}