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++: //?????????P282-286 ????????§????????????

#include <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int UNREACHABLE = -1;

int n, M[N][N];	//M[N][N]?????£??\??????
int d[N];	//???i????????¢???d[i]???????´???????

void bfs(int);

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;
		for(int j=0; j<k; j++){
			cin >> v;
			M[u-1][v-1] = 1;
		}
	}

	//?§???????0??¨?????????????????¢?´¢???????§?
	bfs(0);

	return 0;
}

void bfs(int s){
	queue<int> q;	//?¬?????¨???????????????????????¨??????????????????????????????\???
	q.push(s);

	for(int i=0; i<n; i++){		//???????????????????????¢?´¢??¶??????UNREACHABLE??????????????????
		d[i] = UNREACHABLE;
	}
	
	d[s] = 0;	//?§????????????¢???0???????????????

	int u;	//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] != UNREACHABLE) continue;	//??¢?´¢???????????????????????????
			d[v] = d[u] + 1;
			q.push(v);	//v????????¢?´¢?????£????????§???????????¢?´¢???????????¨??????v????????\??????????????????
		}
	}
	//???????????¨?§??????????????????¢?????¨???
	for(int i=0; i<n; i++){
		cout << i+1 << " " << d[i] << endl;
	}
}

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

int main(void) {
	int n, u, k, i, j, x, t, v;
	bool path[110][110];
	int d[110];
	memset(path, 0, sizeof(path));
	memset(d, -1, sizeof(d));
	cin >> n;
	for (i = 0;i<n;i++) {
		cin >> u >> k;
		u--;
		for (j = 0;j<k;j++) {
			cin >> x;
			x--;
			path[u][x] = true;
		}
	}

	queue<pair<int, int> > q;
	q.push({ 0,0 });
	while (!q.empty()) {
		v = q.front().first;
		t = q.front().second;
		q.pop();
		if (d[v] == -1)d[v] = t;
		else continue;

		t++;
		for (i = 0;i<n;i++) {
			if (path[v][i] && d[i] == -1) {
				q.push({ i,t });
			}
		}
	}

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

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int matrix[110][110];
int dist[110];
int n;
queue<int> Q;

int main() {
  REP(x, 110) {
    REP(y, 110) {
      matrix[x][y] = 0;
    }
    dist[x] = (1 << 15);
  }

  // int n;
  cin >> n;

  REP(_, n) {
    int v, num;
    cin >> v >> num;

    REP(i, num) {
      int cv;
      cin >> cv;
      matrix[v][cv] = 1;
    }
  }

  dist[1] = 0;
  Q.push(1);

  while (!Q.empty()) {
    int f = Q.front(); Q.pop();

    int updated = 0;
    FOR(v, 1, n + 1) {
      if (matrix[f][v]) {
        Q.push(v);
        if (dist[v] > dist[f] + 1) {
          dist[v] = dist[f] + 1;
          updated = 1;
        }
      }
    }
    if (!updated) break;
  }


  FOR(v, 1, n + 1) {
    if (dist[v] == (1 << 15)) {
      cout << v << " -1" << endl;
    } else {
      cout << v << " " << dist[v] << endl;
    }
  }


  return 0;
}

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

using namespace std;

int n,u,k,v,x;
int a[101][101]={};
int d[101]={0};
queue<int> q;

int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> u >> k;
        u-=1;
        for(int j=0;j<k;j++)
        {
            cin >> v;
            v-=1;
            a[u][v]=1;
        }
    }
    q.push(0);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        for(int i=0;i<n;i++)
        {
            if(a[x][i]==0) continue;
            if(d[i]!=0) continue;
            d[i]=d[x]+1;
            q.push(i);
        }
    }
    cout << 1 << " " << 0 << endl;
    for(int i=1;i<n;i++)
    {
        cout << i+1 << " ";
        if(d[i]==0) cout << -1;
        else cout << d[i];
        cout << endl;
    }
    return 0;
}

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



int main(){
  const int MX=1e9+7;
  int a[1000][1000];
  fill(&a[0][0],&a[999][999],MX);
  for(int i=0;i<1000;i++){
    a[i][i]=0;
  }
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    int w;
    cin>>w;
    int m;
    cin>>m;
    for(int t=0;t<m;t++){
      int e;
      cin>>e;
      a[w][e]=1;
    }
  }
  for(int i=1;i<=n;i++){
    for(int t=1;t<=n;t++){
      for(int k=1;k<=n;k++){
	a[t][k]=min(a[t][k],a[t][i]+a[i][k]);
      }
    }
  }
  for(int i=1;i<=n;i++){
    cout<<i<<" "<<(a[1][i]==MX? -1:a[1][i])<<endl;
  }


  
  return 0;
}