Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

C++: #include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <complex>

#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i,n) for(int i=0;i<(n); i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;
typedef long long ll;
const int NMAX = 100;
const int k = 200000;
static const int INF = 1<<21;
string s,t,ans;
int dp[3001][3001];
ll gcd(ll a, ll b){
if(a<b) swap(a,b);
    if(b==0) return a;
    return gcd(b, a%b);
}
int n,M[NMAX+1][NMAX+1]={0};
int d[NMAX]; //距離で訪問状態を管理(d[NMAX]={INFでは初期化できない!?})

void bfs(int s){
    queue<int> q;
    q.push(s);
    rep2(i,n){
        d[i]=INF;
    }
    d[s]=0;
    int u;
    while(!q.empty()){ //空でない限り
        u=q.front();q.pop();
        rep2(v,n){
            if(M[u][v]==0) continue;
            if(d[v]!=INF) continue;
            d[v]=d[u]+1; //距離は今考えているところより1大きい値
            q.push(v);
        }
    }
    rep2(i,n){
        cout << i << " " << ((d[i] == INF) ? (-1): d[i]) << endl;
        //INFのままだったら-1を出力
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0); //この2行はより速度を向上させる
    // 追加２, 以降 cin の入力元が 'input.txt' になる

    int u,v,k;
    cin >> n;
    rep2(i,n){
        int u,k;
        cin >> u >> k;
        int v[k+1];
        rep2(j,k) cin >> v[j];
        rep2(j,k){
            M[i][v[j]]=1;
        }
    }
    bfs(1);
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#define N 100
using namespace std;

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


void bfs(int n) {
	queue<int> Q;
	d[0] = 0;
	Q.push(0);
	while (!Q.empty()) {
	  for (int i = 0;i < n;i++)
		  if (M[Q.front()][i] == 1) {
			  if (color[i] == 0) {
				  d[i] = d[Q.front()] + 1;
				  color[i] = 1;
				  Q.push(i);
			  }
		  }
		  Q.pop();
	}
	for (int i = 0;i < n;i++)
		cout << i + 1 << " " << d[i] << endl;
}


int main() {
	int n;
	cin >> n;
	for (int i = 0;i < n;i++) {
		int u; cin >> u;
		int k; cin >> k;
		for (int j = 0;j < k;j++) {
			int v; cin >> v;
			M[u-1][v-1] = 1;
		}
	}

	bfs(n);

}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define VMAX 50
#define EMAX 500
#define DMAX 20
#define TRUE 1
#define FALSE 0
#define NOTUSED 0
#define END -1
#define ROOT -1
using Graph = vector<vector<int>>;

int main(){
    // 隣接頂点リストの受け取り
    int n;
    cin>>n;

    Graph G(n+1);
    G[0]={NOTUSED};
    REP(i,n){
        int u,k;
        cin>>u>>k;
        G[u].resize(k);
        REP(j,k){
            cin>>G[u][j];
        }
    }

    // BFS のためのデータ構造
    vector<int> d(n+1,-1);
    d[0]=NOTUSED;
    queue<int> que;

    // 初期条件(頂点 1 を初期ノードとする)
    d[1]=0;
    que.push(1);

    // BFS 開始(キューが空になるまで探索を行う)
    while(!que.empty()){
        int v=que.front();
        que.pop();

        // v から辿れる頂点をすべて調べる
        for(int adj_v:G[v]){
            // すでに発見済みの頂点は探索しない
            if(d[adj_v]!=-1) continue;

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            d[adj_v]=d[v]+1;
            que.push(adj_v);
        }
    }

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

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[v] != 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];
    }
  }

  bfs(0);

  return 0;
}