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<queue>

using namespace std;

#define N 100
#define INFTY 99999999

int n,
// 隣接行列
M[N][N];
// 距離 colorも同時に管理する
int d[N];

void bfs(int s){
    queue<int> q;
    q.push(s);
    
    // 距離を初期化
    for(int i = 0;i < n;++i){
        d[i] = INFTY;
    }
    d[s] = 0;
    // 対象の頂点番号
    int u;
    
    while(!q.empty()){
        u = q.front();
        q.pop();
        
        for(int v = 0;v < n;++v){
            // 頂点が隣接しているかつ未訪問
            if(M[u][v] == 1 && d[v] == INFTY){
                // 対象の頂点の距離は前の頂点 + 1
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
    
}

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;
        // 頂点を移動する
        u--;
        // 頂点の数だけ回す
        for(int j = 0;j < k;++j){
            cin >> v;
            --v;    // ずらす
            M[u][v] = 1;
        }
    }
    
    bfs(0);

    for(int i = 0;i < n;++i){
        cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
    }
    
    return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (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] = INFTY;
	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[u] != 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() {
	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 <iostream>
#include <queue>
using namespace std;

void bfs(int);

static const int Q = 100;
static const int W = (1<<21);

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

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

void bfs(int s){
  queue<int> q;
  q.push(s);

  for(int i=0;i<n;i++){
    d[i]=W;
  }
  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]!=W)continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }

  for(int i=0;i<n;i++){
    cout << i+1 << " " << ((d[i]==W)?(-1):d[i]) << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

#define vi vector<int>
#define vl vector<long long>
#define vvi vector< vector<int> >
#define vvl vector< vector<ll> >
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define INF 1e18
#define MOD 1e9+7

template<class T> bool chmax(T &a, const T & b) {
    if (a<b) {
        a = b;
        return true;
    }
    return false;
}

template<class T> bool chmin(T &a, const T & b) {
    if (a>b) {
        a = b;
        return true;
    }
    return false;
}

const int MAX_N = 110;
vector < vector<int> > G;
int d[MAX_N];

void bfs(int s) {
    d[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        REP(i, (int)G[u].size()) {
            int v = G[u][i];
            if (d[v] != -1) continue;
            d[v] = d[u]+1;
            q.push(v);
        }
    }
}

int main() {
    int n;
    cin >> n;
    G.resize(n);
    REP(i, n) {
        d[i] = -1;
        int u, k;
        cin >> u >> k;
        --u;
        REP(j, k) {
            int v;
            cin >> v;
            --v;
            G[u].push_back(v);
        }
    }

    bfs(0);
    REP(i, n) {
        cout << i+1 << ' ' << d[i] << endl;
    }
}

C++: #include <cstdio>

#define N 100

int check(int id, int distance, int a[N + 1][N], int count[N], int d[N]){
    if(d[id] != -1 && distance >= d[id]) return d[id];
    d[id] = distance;
    for(int i = 0; i < count[id]; ++i){
        check(a[id][i], distance + 1, a, count, d);
    }
    return d[id];
}

int main(){
    int n;
    std::scanf("%d", &n);

    int a[N + 1][N];
    int d[N + 1];
    int count[N + 1];
    for(int i = 1; i <= n; ++i){
        int id;
        std::scanf("%d", &id);
        std::scanf("%d", &count[id]);
        for(int j = 0; j < count[id]; ++j){
            std::scanf("%d", &a[id][j]);
        }
        d[id] = -1;
    }

    check(1, 0, a, count, d);

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

    return 0;
}

C++: /*
 * ALDS1_11_C.cpp
 *
 *  Created on: May 8, 2018
 *      Author: 13743
 */

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

using namespace std;

queue<int> Q;
int d[101];

void bfs(vector<int>* a, int rt) {
	Q.push(rt);
	d[rt] = 1;
	while(!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for(int &x:a[u]) {
			if(d[x] == 0) {
				Q.push(x);
				d[x] = d[u] + 1;
			}
		}

	}
}

int main() {
	int n;
	vector<int> adj[101];

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

	bfs(adj, 1);

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

}