Go: package main

/*
グラフの表現 隣接行列による表現
*/

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include<bits/stdc++.h>
#define INF 10000000
using namespace std;

int z[100],n;
int a[100][100];

void bfs(){
  int m;
  z[0]=0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    m=q.front();
    q.pop();
    for(int i=0;i<n;i++){
      if(a[m][i]==1){
	q.push(i);
	z[i]=min(z[i],z[m]+1);
      }
    }
  }
}


int main(){
  int p,x;
  for(int i=0;i<100;i++){
    z[i]=INF;
    for(int j=0;j<100;j++) a[i][j]=0;
  }
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>p>>p;
    for(int j=0;j<p;j++){
      cin>>x;
      a[i][x-1]=1;
    }
  }
  bfs();
  for(int i=0;i<n;i++){
    if(z[i]!=INF) cout<<i+1<<" "<<z[i]<<endl;
    else cout<<i+1<<" "<<-1<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int n,m[101][101];
int d[101];
void bfs(int s){
  queue<int>q;
  q.push(s);
  for(int i=0;i<n;i++){
    d[i]=INF;
  }
  d[s]=0;
  while(!q.empty()){
    int u=q.front();
    q.pop();
    for(int v=0;v<n; v++){
      if(m[u][v]==0){
	continue;
      }
      if(d[v]!=INF){
	continue;
      }
      d[v]=d[u]+1;
      q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF)?(-1):d[i] )<<endl;
  }
}
int main(){
  int u,k,v;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
      cin>>v;
      m[u-1][v-1]=1;
    }
  }
  bfs(0);
  return(0);
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int G[101][101];
int d[101];

void explore(int id, int n,int cnt)
{
	if (d[id] == -1)
	{
		d[id] = cnt;
	}
	else
	{
		d[id] = min(d[id], cnt);
	}

	for (int i = 1; i <= n; i++)
	{
		if (G[id][i] == 1)
		{
			explore(i, n, cnt + 1);
		}
	}
}

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

	for (int i = 0; i < n; i++)
	{
		int id, k;
		cin >> id >> k;

		for (int i = 0; i < k; i++)
		{
			int v;
			cin >> v;
			G[id][v] = 1;
		}
	}

	for (int i = 1; i <= 100; i++)
	{
		d[i] = -1;
	}

	explore(1, n, 0);

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

	return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 100000

static const int N =100;
static const int INFTY =(1<<21);

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]==1){
        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>
//#define int long long
//#define _DEBUG
#include <queue>
#include <vector>
using namespace std;
int N, T[107][107], D[107]; //T[i][j]:i->T[i][j]の辺
int const infty = -1, none = -9;
queue<int> Q;
void solve(){
    Q.push(0);
    D[0] = 0;
    while(true){
#ifdef _DEBUG
        cout << "Q1 ";
        int length = Q.size();
        for(int k=0; k<length; k++){
            int qf = Q.front(); Q.pop();
            cout << qf << " ";
            Q.push(qf);
        }cout << endl; 
#endif
        int top = Q.front(); Q.pop();
#ifdef _DEBUG
        cout << "top " << top << endl << "Q2 ";
        length = Q.size();
        for(int k=0; k<length; k++){
            int qf = Q.front(); Q.pop();
            cout << qf << " ";
            Q.push(qf);
        }cout << endl; 
#endif
        for(int j=0; j<N; j++){
#ifdef _DEBUG
            cout << j << ", " << T[top][j] << endl;
            cout << "top " << top << endl << "Q3 ";
            length = Q.size();
            for(int k=0; k<length; k++){
                int qf = Q.front(); Q.pop();
                cout << qf << " ";
                Q.push(qf);
            }cout << endl; 
#endif
            if(T[top][j] == none)break;
            if(D[ T[top][j] ] != infty)continue;
            Q.push( T[top][j] );
            D[ T[top][j] ] = D[top]+1;
        }
#ifdef _DEBUG
        cout << "D " ;
        for(int k=0; k<N; k++) cout <<D[k]<<" ";
        cout << endl;
#endif
        if(Q.empty()) break;
    }
}
void get(){
    cin >> N;
    for(int i=0; i<N; i++){
        D[i] = infty;
        for(int j=0; j<N; j++){
            T[i][j] = none;
        }
    }
    for(int i=0; i<N; i++){
        int i_, k;
        cin >> i_ >> k;
        for(int j=0; j<k; j++){
            int vj;
            cin >> vj;
            T[i][j] = vj-1;
        }
    }
}

signed main(){
    get();
    
#ifdef _DEBUG
    for(int k=0; k<N; k++) cout << D[k] << " ";
    cout << endl;
    for(int k=0; k<N; k++){
        for(int l=0; l<N; l++){
            if(T[k][l] == none)break;
            cout << T[k][l] << " ";
        }
        cout << endl;
    }
#endif
    solve();
    for(int i=0; i<N; i++){
        cout << i+1 << " " << D[i] << endl;
    }
}