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 <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 100;
struct Graph{
    struct Edge{
        int to, w;
        Edge(int v, int _w):to(v), w(_w){}
    };
    vector<Edge> edge;
    vector<vector<int> > vertex;
    int V, E;
    Graph(int _V):V(_V),E(0){
        edge.clear(); vertex.clear();
        vertex.resize(V);
    }
    void addEdge(int u, int v, int w){
        vertex[u].push_back(E++);
        edge.push_back(Edge(v, w));
    }
};
void SPFA(Graph& G, LL* d, int s){
    bool inq[MAXN]; int cnt[MAXN];
    memset(cnt, 0, sizeof(int)*G.V);
    memset(d, INF, sizeof(LL)*G.V);
    memset(inq, false, sizeof(bool)*G.V);
    d[s] = 0; inq[s] = true;
    queue<int> q; q.push(s);
    while (q.size()){
        int p = q.front(); q.pop(); inq[p] = false;
        cnt[p]++; if (cnt[p] > G.V){ d[s] = -1; break;}
        for (auto index : G.vertex[p]){
            auto &e = G.edge[index];
            if (d[p] + e.w < d[e.to]){
                d[e.to] = d[p] + e.w;
                if (!inq[e.to]) inq[e.to] = true, q.push(e.to);
            }
        }
    }
}
int main(){
    int n;
    cin >> n;
    Graph G(n);
    LL d[MAXN];
    for (int i = 0 ; i < n ; i++){
        int m;
        cin >> m >> m;
        while (m--){
            int v;
            cin >> v;
            G.addEdge(i, v-1, 1);
        }
    }
    SPFA(G, d, 0);
    for (int i = 0 ; i < n ; i++)
        cout << i+1 << ' ' << d[i] << '\n';
}

C++: #include<bits/stdc++.h>

using namespace std;
int V;
vector<int> G[102];
bool vis[102];
int d[102];
const int inf=0x3f3f3f;

void bfs(int s=1){
    d[s]=0;
    queue<int> que;
    que.push(s);
    while(!que.empty()){
        int t=que.front();
        que.pop();
        vis[t]=true;
        for(int i=0;i<G[t].size();i++){
            if(!vis[G[t][i]]){
                que.push(G[t][i]);
                d[G[t][i]]=min(d[G[t][i]],d[t]+1);
            }
        }
    }
}


int main(){
    cin>>V;
	int i;
	for(int i=1;i<=V;i++){
        d[i]=inf;
        vis[i]=false;
	}
    for(i=1;i<=V;i++){
        int id,k;
        cin>>id>>k;
        while(k--){
            int out;
            cin>>out;
            G[id].push_back(out);
        }
        sort(G[id].begin(),G[id].end());
    }
    bfs();
    for(int i=1;i<=V;i++){
        cout<<i<<" "<<(d[i]==inf?-1:d[i])<<endl;
    }
    return 0;
}

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

int main() {
  ll n;
  cin >> n;
  vector<set<ll>> v(n);
  for(ll i=0;i<n;i++) {
    ll f,k;
    cin >> f >> k;
    for(ll j=0;j<k;j++) {
      ll temp;
      cin >> temp;
      temp--;
      v[i].insert(temp);
    }
  }
  vector<ll> ans(n,-1);
  ans[0]=0;
  queue<ll> q;
  q.push(0);
  while(q.size()) {
    ll p=q.front();
    q.pop();
    for(ll t:v[p]) {
      if(ans[t]<0) {
        ans[t]=ans[p]+1;
        q.push(t);
      }
    }
  }
  for(ll i=0;i<n;i++) {
    cout << i+1 << " " << ans[i] << endl;
  }
}

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

using namespace std;

int main(){
    int n;
    cin >> n;

    vector<vector<int>> graph(n);

    for(int i = 0; i < n; i++ ){
        int v, k;
        cin >> v >> k;
        for(int j = 0; j < k; j++){
            int v2;
            cin >> v2;
            graph[v-1].push_back(v2-1);
        }
    }

    vector<int> d(n, -1);
    queue<int> q;
    q.push(0);
    d[0] = 0;

    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(int i = 0; i < graph[cur].size(); i++ ){
            int next = graph[cur][i];
            if(d[next] == -1){
                d[next] = d[cur]+1;
                q.push(next);
            }
        }
    }

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

    return 0;
}

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

using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int INF = 1 << 30;

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


void dfs(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(M[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++)
        {
            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;
        }
    }

    dfs(0);

    return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 100000

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]==1){
        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;
}