Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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