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;
    queue<int> Q;
int u[1000][1000];
int d[1000],color[1000];
// white 0 gray 1 black 2
void BFS(int s,int n){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            color[i]=0;
            d[i]=-1;
        }
    }
    color[s]=1;
    d[s]=0;
    Q.push(s);
    while(Q.size()!=0){
        int tmp=Q.front();
        Q.pop();
        for(int j=1;j<=n;j++){
            if(color[j]==0 && u[tmp][j]==1){
                color[j]=1;
                d[j]=d[tmp]+1;
                Q.push(j);
            }
        }
        color[tmp]=2;
    }
}
int main(){
    int n,l,k,m;
    cin>>n;
    u[100][100]={};
    for(int i=0;i<n;i++){
        cin>>l>>k;
        for(int j=1;j<=k;j++){
            cin>>m;        
            u[l][m]=1;
        }
    }
    BFS(1,n);
    for(int i=1;i<=n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
    return 0;
}

C++: // Ki
// 所要時間 25分 (1WA)
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define REP(i, d, n) for(int i=(d); i<(n); ++i)
#define all(v) v.begin(), v.end()

int main() {

    int n;
    cin >> n;
    vector<vector<int> > graph(n);
    rep(i, n){
        int u, k;
        cin >> u >> k;
        --u;
        rep(j, k){
            int v;
            cin >> v;
            graph[u].push_back(--v);
        }
    }

    queue<int> q;
    vector<int> dis(n, -1);
    q.push(0);
    dis[0] = 0;
    while(!q.empty()){
        int v = q.front();
        q.pop();
        rep(i, graph[v].size()){
            int u = graph[v][i];
            if(dis[u] == -1){
                dis[u] = dis[v] + 1;
                q.push(u);
            }
        }
    }

    rep(i, n){
        cout << i+1 << " " << dis[i] << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

static const int MAX_NUM = 101;

void BFS(int matrix[][MAX_NUM], int num, int target, int result[])
{
    queue<int> Q;
    int  distance = 0;
    char state[MAX_NUM] = {0};

    Q.push(target);
    result[target] = distance;
    state[target] = 1;

    while (Q.empty() == false) {
        int index = Q.front();
        Q.pop();

        for (int i = 1; i < num; i++) {
            if ((matrix[index][i] == 1) && (state[i] == 0)) {
                Q.push(i);
                state[i]  = 1;
                result[i] = result[index] + 1;
            }
        }
    }

    return;
}

int main(void)
{
    int num;
    int index, elemNum, tmp;
    int matrix[MAX_NUM][MAX_NUM] = {0};
    int result[MAX_NUM];

    scanf("%d", &num);
    for (int i = 1; i < num + 1; i++) {
        result[i] = -1;
        scanf("%d %d", &index, &elemNum);
        for (int j = 0; j < elemNum; j++) {
            scanf("%d", &tmp);
            matrix[index][tmp] = 1;
        }
    }

    BFS(matrix, num, 1, result);

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

    return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vi> g(n);
  REP(i, n) {
    int u, k;
    cin >> u >> k;
    u--;
    REP(i, k) {
      int v;
      cin >> v;
      v--;
      g[u].push_back(v);
    }
  }

  queue<pair<int, int>> q;
  vi ans(n, -1);
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    if (ans[p.first] != -1)
      continue;
    ans[p.first] = p.second;
    for (auto v : g[p.first]) {
      q.push(make_pair(v, ans[p.first] + 1));
    }
  }

  REP(i, n) { cout << i + 1 << " " << ans[i] << endl; }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <climits>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#define MAX 1000
#define INFTY 1<<30 // 2^30
using namespace std;
typedef long long llong;

static const int N = 100;

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

int 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 not connect
      if(d[v] != INFTY) continue; // if have reached
      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++)
  {
    cin >> u >> k;
    u--;
    for(int j = 0; j < k; j++)
    {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
}