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++: //
#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;
}

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

static const int N = 100;
static const int no = -1;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[N][N] = {0}, d[N] = {0}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for(int i = 1; i <= n; i++) d[i] = no;
  d[v] = 0;
  while(!q.empty()) {
    u = q.front(); q.pop();
    for(int i = 1; i <= n; i++) {
      if((graph[u][i] == 0) || (d[i] != no)) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
	ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

	return 0;
}