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 <iostream>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int GREY = 1;
static const int BLACK = 2;

int n, m[N][N];
bool visited[N];
int d[N], dist;


void bfs(int i){
	queue<int> q; q.push(i);
	visited[i] = true;

	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int v = 0; v < n; ++v) {
			if (!visited[v] && m[u][v]){
				d[v] = d[u] + 1;
				visited[v] = true;
				q.push(v);
			}
		}
	}
}


void search(){
	for (int i = 0; i < n; ++i) {
		d[i] = -1;
		visited[i] = false;
	}
	d[0] = 0;
	bfs(0);

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


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

	search();

	return 0;
}

C++: #include <iostream>
#include <queue>
static const int MAX = 100;
static const int INFTY = (1<<21);
using namespace std;

enum Color {
  WHITE, GRAY, BLACK
};

int n;
int G[MAX][MAX];
int d[MAX];
Color color[MAX];


void bfs_init() {
  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    d[i] = INFTY;
  }
}

void bfs(int r) {
  bfs_init();
  queue<int> q;
  q.push(r);
  d[r] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (G[u][v] == 0) continue;
      if (d[v] != INFTY) continue;
      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() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      G[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    int vi, out_v_num;
    cin >> vi;
    cin >> out_v_num;
    for (int j = 0; j < out_v_num; j++) {
      int out_vi;
      cin >> out_vi;
      G[vi-1][out_vi-1] = 1;
    }
  }
  bfs_init();
  bfs(0);
  return 0;
}

C++: ///
// File:  alds1_11_c.cpp
// Author: ymiyamoto
//
// Created on Wed Oct 18 00:49:05 2017
//

#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
static uint32_t graph[100][100];
static vector<int32_t> d(100, -1);
static queue<uint32_t> q;

static void bfs(uint32_t n)
{
  q.push(0);
  d[0] = 0;

  while (!q.empty()) {
    uint32_t index = q.front();
    q.pop();

    for (uint32_t i = 0; i < n; i++) {
      if (d[i] == -1 && graph[index][i] == 1) {
        q.push(i);
        d[i] = d[index] + 1;
      }
    }
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t id, k;
    cin >> id >> k;
    for (uint32_t j = 0; j < k; j++) {
      uint32_t val;
      cin >> val;
      graph[id - 1][val - 1] = 1;
    }
  }

  bfs(n);

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

  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
#define N (100)
#define INFINITY (1<<21)

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

void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=0;i<n;i++){
        d[i]=INFINITY;
    }
    d[s]=0;
    int 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]!=INFINITY)continue;
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<((d[i]==INFINITY)?(-1):d[i])<<endl;
    }
}

int main(void){
    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 <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

int	main()
{
	int	n;
	cin >> n;
	Graph	graph(n);
	int	u;
	for (int i = 0; i < n; ++i) {
		int	k, v;
		cin >> u >> k;
		--u;
		for (int j = 0; j < k; ++j) {
			cin >> v;
			--v;
			graph.at(u).push_back(v);
		}
	}
	vector<int>	cost(n, -1);
	queue<int>	que;
	cost.at(0) = 0;
	que.push(0);
	int	cost_u;
	while (!que.empty()) {
		u = que.front();
		que.pop();
		cost_u = cost.at(u);
		for (int v : graph.at(u)) {
			if (cost.at(v) != -1) continue;
			cost.at(v) = cost_u + 1;
			que.push(v);
		}
	}
	for (int i = 0; i < n; ++i) {
		cout << i + 1 << " " << cost.at(i) << endl;
	}
}