Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

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

using namespace std;

void bfs(vector<vector<int>> matrix) {
	
	//初期化
	//始点からの距離は1次元vectorで管理
	//始点から各頂点への距離をinf(-1)に設定
	int n = matrix.size();
	vector<int> d(n, -1);
	
	//始点をキューに入れる。始点の距離を0に
	queue<int> Q;
	Q.push(0);
	d.at(0) = 0;

	//以下ループ処理で距離の算出
	//キューが空になるまでこのループを回す
	while(! Q.empty()) {
		//キューの先頭の頂点を取り出す
		int head = Q.front();
		Q.pop();
		//その頂点から行ける点について-1なら
		for (int i = 0; i < matrix.at(0).size(); i++) {
			if (matrix[head][i] == 1 && d.at(i) == -1) {
				Q.push(i);
				d.at(i) = d.at(head) + 1;
			}
		}
	}

	// 表示
	for (int j = 0; j < d.size(); j++) {
		cout << j + 1 << " " << d.at(j) << endl;
	}
}

int main(void) {

	//標準入力を受け取る
	int n; //頂点数
	cin >> n;
	vector<vector<int>> matrix(n, vector<int>(n,0)); //隣接行列
	int u, k, v; //頂点番号、出次数、隣接する頂点番号
	for(int i = 0; i < n; i++) {
		cin >> u >> k;
		while(k--) {
			cin >> v;
			matrix.at(u-1).at(v-1) = 1; //有向グラフならこれだけ
			// matrix.at(v-1).at(u-1) = 1; //無向グラフなら反対向きも
		}
	}

	bfs(matrix);
	//
	// 隣接行列の表示
	// for(int i = 0; i < matrix.size(); i++) {
	//	 for(int j = 0 ;j < matrix.at(0).size(); j++) {
	//	 	cout << matrix.at(i).at(j) << " ";
	//	 }
	//	cout << endl;
	// }
}

//隣接リストどうやるの？

C++: #include <iostream>
#include <stdio.h>
#include <queue>
#define N 101
#define INFTY (1<<21)


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

void bfs(int u) {
    std::queue<int> Q;
    Q.push(u);
    for (int i = 0; i < n; i++) d[i] = INFTY;
    d[u] = 0;
    while ( ! Q.empty() ) {
        int j = Q.front();
        Q.pop();
        for (int v = 0; v < n; v++) {
            if (M[j][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[u] + 1;
            Q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}

int main() {
    int u, k, v;
    scanf("%d", &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++) {
        scanf("%d %d", &u, &k);
        u--;
        if (k == 0) continue;
        for (int j = 0; j < k; j++) {
            scanf("%d", &v);
            v--;
            M[u][v] = 1;
        }
    }
    
    bfs(0);
    
    return 0;
}

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;
}