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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

int main() {
  ll input(n);
  vector<vector<ll>> graph(n);
  REP(i, n) {
    ll input(u, k);
    REP(j, k) {
      ll input(v);
      graph[u - 1].push_back(v - 1);
    }
  }

  vector<bool> visited(n);
  vector<ll> cost(n);
  queue<pair<ll, ll>> que;
  que.push({0, 0});
  while (!que.empty()) {
    pair<ll, ll> node = que.front();
    que.pop();

    if (!visited[node.first]) {
      cost[node.first] = node.second;
    }
    visited[node.first] = true;

    for(auto v: graph[node.first]) {
      if (!visited[v]) {
        que.push({v, node.second + 1});
      }
    }
  }
  REP(i, n) {
    if (!visited[i]) cost[i] = -1;
    cout << i + 1 << " " << cost[i] << endl;;
  }
}

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

using namespace std;

int d[100];
vector<int> adj[100];

void bfs(int n, int start) {
	queue<int> que;

	for (int i = 0; i < n; i++)
		d[i] = (i == start ? 0 : -1);

	que.push(start);
	while(!que.empty()) {
		int from = que.front();
		que.pop();

		for (int i = 0; i < adj[from].size(); i++) {
			int to = adj[from][i];
			if (d[to] == -1) {
				d[to] = d[from] + 1;
				que.push(to);
			}
		}
	}
}

int main() {
	int n;

	cin >> n;

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

	bfs(n, 0);

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

	return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

#define MAX_N (105)

int n;
bool G[MAX_N][MAX_N];
int dist[MAX_N];
int main()
{
    queue<int> q;
    scanf("%d", &n);
    for ( int i = 1; i <= n; i++ ) {
	int u, k;
	scanf("%d %d", &u, &k);
	for ( int j = 0; j < k; j++ ) {
	    int d;
	    scanf("%d", &d);
	    G[u][d] = true;
	}
    }

    memset(dist, -1, (n+1) * sizeof(int));

    dist[1] = 0;
    q.push(1);
    
    while ( !q.empty() ) {
	int cur = q.front(); q.pop();
	for ( int i = 1; i <= n; i++ ) {
	    if ( G[cur][i] && (dist[i] == -1) ) {
		dist[i] = dist[cur] + 1;
		q.push(i);
	    }
	}
    }

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

    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
#define NIL -2000000001
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n = 0;
int adl[MAX][MAX], deg[MAX];
int adm[MAX][MAX];
int color[MAX];
int d[MAX];
queue<int> queueBfs;


void bfs() {
  for ( int i = 0; i < n; i++ ) {
    color[i] = WHITE;
    d[i] = -1;
  }
  queueBfs.push(0);
  color[0] = GRAY;
  d[0] = 0;

  while (!queueBfs.empty()) {
    int top = queueBfs.front();

    for (int i = 0; i < n; i++) {
      if (adm[top][i] == 1 && color[i] == WHITE) {
        queueBfs.push(i);
        color[i] = GRAY;
        d[i] = d[top]+1;
      }
    }

    queueBfs.pop();
  }
}

int main() {
  scanf("%d", &n);
  for ( int i = 0; i < n; i++ ) {
    for ( int j = 0; j < n; j++ ) {
      adm[i][j] = 0;
    }
  }
  for ( int i = 0; i < n; i++ ) {
    int num;
    scanf("%d", &num);
    num--;
    scanf("%d", &deg[num]);
    for ( int j = 0; j < deg[num]; j++) {
      scanf("%d", &adl[num][j]);
      adl[num][j]--;
      adm[num][adl[num][j]] = 1;
    }
  }

  bfs();

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



  return 0;
}

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

const int Max = 105;
int map[Max + 1][Max + 1];

typedef struct{
	bool startflag;
	int distance;
}State;

int main()
{
	int n, k, l, temp;
	std::queue<int> q;
	std::vector<State> state;

	for (int i = 1; i < Max + 1; i++){
		for (int j = 1; j < Max + 1; j++){
			map[i][j] = 0;
		}
	}

	std::cin >> n;
	for (int i = 1; i <= n; i++){
		std::cin >> k >> l;
		for (int j = 1; j <= l; j++){
			std::cin >> temp;
			map[k][temp] = 1;
		}
	}

	State s = { false, 0 };
	for (int i = 0; i <= n; i++){
		state.push_back(s);
	}

	int d = 0;
	while (1){
		if (q.empty() == true){
			bool f = false;
			for (int i = 1; i <= n; i++){
				if (state[i].startflag == false){
					q.push(i);
					state[i].startflag = true;
					d = 0;
					state[i].distance = d;
					d++;
					f = true;
					break;
				}
			}
			if (f == false){
				break;
			}
		}
		int size = q.size();
		for (int j = 0; j < size; j++){
			for (int i = 1; i <= n; i++){
				if (map[q.front()][i] == 1 && state[i].startflag == false){
					q.push(i);
					state[i].startflag = true;
					state[i].distance = d;
				}
			}
			q.pop();
		}
		d++;
	}

	for (int i = 1; i <= n; i++){
		std::cout << i << " " << state[i].distance << std::endl;
	}

	return 0;
}