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<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2



int d[MAX];
vector<int> V[MAX];
int tt=0;
queue<int> Q;

int main()  {
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    d[i]=-1;
  }

  int u,k,v;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    u--;
    for(int j=0;j<k;j++){
      cin>>v;
      v--;
      V[u].push_back(v);
    }
  }

  Q.push(0);
  d[0]=tt;

  while(!Q.empty()){
    v=Q.front(); Q.pop();
    for(int i=0;i<V[v].size();i++){
      if(d[V[v][i]]==-1) {
        Q.push(V[v][i]);
        d[V[v][i]]=d[v]+1;

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

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

using namespace std;

int n, M[100][100], d[100];

void bfs (int s) {

    int i, u, v;

    queue<int> Q;
    Q.push(s);
    for (i = 0; i < n; i++) {
        d[i] = (1 << 21);
    }
    d[s] = 0;
    while (!q.empty()) {
        u = Q.front();
        Q.pop();
        for (v = 0; v < n; v++) {
            if (M[u][v] == 0) {
                continue;
            }
            if (d[v] != (1 << 21)) {
                continue;
            }
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (i = 0; i < n; i++) {
        cout << i + 1 << " " << ((d[i] == (1 << 21)) ? (-1) : d[i]) << endl;
    }
}

int main () {

    int u, k, v, i, j;

    cin >> n;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }

    for (i = 0; i < n; i++) {
        cin >> u >> k;
        u--;
        for (j = 0; j < k; j++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs (0);

    return 0;

}

C++: #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

bool A[110][110],flag[110];
int d[110];
int n;

void bfs(){
	queue<int> Q;
	Q.push(1);
	d[1]=0;
	while(Q.empty()==0){
		int u=Q.front();
		Q.pop();
		for(int i=2;i<=n;i++){
			if(A[u][i]==true && flag[i]==false ){
				flag[i]=true;
				Q.push(i);
				d[i]=d[u]+1;
			}
		}
	}
	for(int i=2;i<=n;i++) if(flag[i]==false) d[i]=-1;
	return ;
}

int main(){
	int a,b,c;
	cin>>n;
	for(int i=1;i<=n;i++){
		flag[i]=false;
			for(int j=1;j<=n;j++){
				A[i][j]=false;
		}
	}
	for(int i=1;i<=n;i++){
		cin>>a>>b;
		for(int j=1;j<=b;j++){
			cin>>c;
			A[a][c]=true;
		}
	}
	bfs();
	for(int i=1;i<=n;i++) cout<<i<<" "<<d[i]<<endl;	
	return 0;
}

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

void bfs(vector<vector<int>> a, vector<int> &d) {
	int n = a.size();
	queue<int> qu;
	qu.push(0);
	while (!qu.empty()) {
		int i = qu.front();
		qu.pop();
		REP(j, n) {
			if (d[j] == -1 && a[i][j]) {
				d[j] = d[i] + 1;
				qu.push(j);
			}
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n, 0));
	REP(i, n) {
		int u, k;
		cin >> u >> k;
		u--;
		REP(j, k) {
			int v;
			cin >> v;
			a[u][--v] = 1;
		}
	}
	vector<int> d(n, -1);
	d[0] = 0;
	bfs(a, d);
	REP(i, n) {
		cout << i + 1 << " " << d[i] << "\n";
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cstdio>
#include<queue>

using namespace std;

int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    fill(D, D + N, -1);
    D[src] = 0;
    while (! Q.empty()){
        int cur = Q.front();
        Q.pop();
        cerr << cur+1 << " " << D[cur] << endl;
        for (int k = 0; k < N; k++){
            if (G[cur][k] == 1 && D[k] < D[cur]){
                D[k] = D[cur]+1;
                Q.push(k);
            }
        }
    }
}

int main(){
	scanf("%d", &N);
		int id, c;
		for (int i = 0; i < N; i++){
			scanf("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
			scanf("%d", V + id);
			for (int j = 0; j < V[id]; j++){
				scanf("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
				G[id][c] = 1;
			}
		}//データの読み込み完了。
    bfs(0);
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<algorithm>

using namespace std;

queue<int> Q;
int N;
bool PATH[101][101] = {false};
int len[101];

void BFS(int id){
    Q.pop();
    for (int i = 1; i <= N; i++) {
        if (PATH[id][i]) {
            if (len[i] > len[id] + 1) {
                len[i] = len[id] + 1;
                Q.push(i);
            }
        }
    }
}

void BFS_m(){
    while (!Q.empty()) {
        BFS(Q.front());
    }
}

int main(){
    Q.push(1);
    cin >> N;
    for (int i = 1; i <= N; i++) {
        int bb;
        int n;
        cin >> bb;
        cin >> n;
        for (int k = 0; k < n; k++) {
            int s;
            cin >> s;
            PATH[i][s] = true;
        }
    }
    for (int i = 1; i <= N; i++) len[i] = 2000000000;
    len[1] = 0;
    
    BFS_m();
    
    for (int i = 1; i <= N; i++) {
        if (len[i] == 2000000000) len[i] = -1;
        printf("%d %d\n",i,len[i]);
    }
    
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<utility>
#include<numeric>
#include<vector>
#include<map>
#include<set>
#include<tuple>
#include<stack>
#include<queue>
#include<functional>
#include<iterator>
#include<cmath>

using namespace std;

typedef pair<int,int> P;


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

	vector<int> es[n];
	int u,k;
	for(int i=0;i<n;i++){
		cin >> u >> k;
		int v;
		for(int j=0;j<k;j++){
			cin >> v;
			es[i].push_back(v-1);
		}
	}

	int d[n];
	fill(d,d+n,-1);
	queue<P> que;
	que.push(P(0,0)); //点番号（0オリジン）、距離
	d[0] = 0;
	while(!que.empty()){
		P t = que.front(); que.pop();
		for(int i=0;i<es[t.first].size();i++){
			if(d[es[t.first][i]] == -1){
				que.push(P(es[t.first][i],t.second+1));
				d[es[t.first][i]] = t.second+1;
			}
		}
	}

	for(int i=0;i<n;i++){
		cout << i+1 << " " << d[i] << "\n";
	}
	return 0;
}