Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

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

static const int N = 100;
static const int INF = 100000000000;

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


void bfs(int s) {
	

	queue<int> q;
	q.push(s);
	
	for (int i=0; i< n;i++)d[i] = INF;
	d[s] = 0;
	int u;

	while (!q.empty()) {
		u = q.front(); q.pop();
		for(int v=0;v<n;v++){
			if (Adj[u][v] == 0) continue;
			if (d[v] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
			}
		}
	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((d[i] == INF) ? (-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++) {
			Adj[i][j] = 0;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++) {
			cin >> v;
			v--;
			Adj[u][v] = 1;
		}
	}
	
	bfs(0);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int G[110][110];
bool seen[110];
int d[110];
int N;

struct MyQueue{
    int num[110];
    int head = 0;
    int tail = 0;

    void enqueue( int n ){
        num[tail++] = n;
    }

    int dequeue(){
        return num[head++];
    }

    bool empty(){
        return head == tail;
    }
};

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

    MyQueue que;
    que.enqueue( 0 );
    seen[0] = 1;
    while( !que.empty() ){
        int u = que.dequeue();
        for( int v = 0; v < N; v++ ){
            if( G[u][v] && !seen[v] ){
                que.enqueue( v );
                d[v] = d[u] + 1;
                seen[v] = 1;
            }
        }
    }

    for( int i = 1; i < N; i++ ){
        if( d[i] == 0 ) d[i] = -1;
    }

    for( int i = 0; i < N; i++ ){
        cout << i + 1 << " " << 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] = 1;
    }
  }

  bfs(0);

  return 0;
}

C++: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <iomanip>
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const int INF = 1000000000;
const int MOD = 1000000007;

typedef pair<int, int> P;
int myAbs(int num) {
    if (num > 0)
        return num;
    else
        return -num;
}

long long gcd(long long a, long long b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a * b / gcd(a, b);
}

using Graph = vector<vector<int>>;
//再帰とかグラフを使うときは予めNの最大値のグローバル配列を取っておく
int N;
bool E[101][101];
vector<bool> seen;
typedef pair<int, int> P;
int D[101];
//頂点状態を表すcolor[]において、0は未訪問、1は訪問中、2は完了を表す。
int color[101];
int find_time = 0;

void bfs(int start) {
    //全頂点を未訪問にして距離も初期化(INF)
    for (int i = 0; i < 101; i++) D[i] = -1;
    queue<int> Q;
    //最初の頂点をキューに入れる
    Q.push(start);
    int v;
    //最初の頂点は距離0
    D[start] = 0;
    //キューが空になるまで探索
    while (!Q.empty()) {
        v = Q.front();
        Q.pop();
        for (int i = 1; i <= N; i++) {
            //今訪問している頂点vから辺があるか&&すでに訪問済み(距離がINFではない)を確認
            if (!E[v][i]) continue;
            if (D[i] != -1) continue;
            //未訪問かつ辺があったなら元の頂点の距離に1を足す
            D[i] = D[v] + 1;
            //発見した頂点をキューに入れて再度探索
            Q.push(i);
        }
    }
    for (int i = 1; i <= N; i++) {
        cout << i << " " << D[i] << endl;
    }
}

int main() {
    cin >> N;
    int u, k,v;
    //頂点を1から入力(0ではなく)
    //辺の有無をbool E[][]で管理
    for (int i = 0; i < N; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> v;
            E[u][v] = true;
        }
    }
    //探索開始頂点を引数にして開始
    bfs(1);

}

C++: #include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
int g[101][101], d[101];
bool vis[101];
int dis = 0, n;

void bfs(int u)
{
  queue<int> Q;
  Q.push(u);
  vis[u] = true;
  d[u] = 0;
  while (!Q.empty())
  {
    int tmp = Q.front();
    Q.pop();
    for (int i = 0; i < n; i++)
    {
      if (g[tmp][i] != 0 && !vis[i])
      {
        vis[i] = true;
        Q.push(i);
        d[i] = d[tmp] + 1;
      }
    }
  }
}

int main()
{
  memset(g, 0, sizeof(g));
  memset(d, -1, sizeof(d));
  memset(vis, false, sizeof(vis));
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
  {
    int u, adj_num;
    scanf("%d%d", &u, &adj_num);
    while (adj_num--)
    {
      int v;
      scanf("%d", &v);
      g[u - 1][v - 1] = 1;
    }
  }
  bfs(0);
  for (int i = 0; i < n; i++)
  {
    printf("%d %d\n", i + 1, d[i]);
  }
}