Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

C++: #include<cstdio>
#include<queue>
using namespace std;
static const int INF=(1<<21);
static const int MAXN=101;
int main()
{
	int n,m[MAXN][MAXN],i,j,u,k,v,l,o,c=1,t[MAXN],z=2;
	queue<int> q;
	bool used[MAXN]={};
	scanf("%d",&n);
	for(i=1;i<n+1;i++){
		for(j=1;j<n+1;j++){
			m[i][j]=INF;
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		for(j=0;j<k;j++){
			scanf("%d",&v);
			m[u][v]=1;
		}
	}
	q.push(1);
	used[1]=true;
	t[1]=0;
	while(1){
		l=q.front();
		q.pop();
		o=0;
		for(i=1;i<n+1;i++){
			if(m[l][i]==1&&used[i]==false){
				q.push(i);
				used[i]=true;
				t[i]=c;
				o=1;
			}
		}
		if(o==1){
			c++;
		}
		if(q.empty()){
			z=0;
			for(i=1;i<n+1;i++){
				if(used[i]==false){
					q.push(i);
					z=1;
					used[i]=true;
					break;
				}
			}
		}
		if(z==0){
			break;
		}
		if(z==1){
			c++;
			z=2;
		}
	}
	for(i=1;i<n+1;i++){
		printf("%d %d\n",i,t[i]);
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define white 0
#define black -1
#define grey 1
using namespace std;
int a[110][110],n;
int d[110];
int color[110];

void bfs()
{
    memset(d,0,sizeof(d));
    queue<int> Q;
    d[1]=0;
    for(int i=1;i<=100;i++)
        color[i]=white;
    Q.push(1);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        for(int i=1;i<=n;i++)
        {
            if(a[u][i]==0)
                continue;
            if(d[i]!=0)
                continue;
                Q.push(i);
                color[i]=grey;
                d[i]=d[u]+1;
        }
    }
    for(int i=1;i<=n;i++)
   {
       printf("%d ",i);
       if(i==1) printf("%d",d[i]);
       else if(d[i]) printf("%d",d[i]);
       else printf("-1");
       printf("\n");
   }

}

int main()
{
   int key,d,x;
   memset(a,0,sizeof(a));
   scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {
       scanf("%d%d",&key,&d);
       for(int j=1;j<=d;j++)
       {
        scanf("%d",&x);
        a[key][x]=1;
       }
   }
   bfs();
   return 0;
}

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

typedef long long ll;

int main()
{
    ll i,j;
    ll n;
    cin >> n;
    vector<vector<ll>> graph(n);
    for(i = 0;i < n;++i){
        ll u,k,v;
        cin >> u >> k;
        --u;
        for(j = 0;j < k;++j){
            cin >> v;
            --v;
            graph.at(u).push_back(v);
        }
    }
    queue<ll> que;
    vector<ll> visit(n,false);
    que.push(0);
    visit.at(0) = true;
    vector<ll> ans(n,-1);
    ans.at(0) = 0;
    while(!que.empty()){
        ll s = que.front();
        que.pop();
        ll dis = ans.at(s);
        for(i = 0;i < graph.at(s).size();++i){
            ll g = graph.at(s).at(i);
            if(visit.at(g)){
                continue;
            }
            ans.at(g) = dis+1;
            visit.at(g) = true;
            que.push(g);
        }
    }
    for(i = 0;i < n;++i){
        cout << i+1  << " " << ans.at(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[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>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

int main() {
	int n, u, k, v;
	cin >> n;
	vector<int> ans(n,1000000000);
	vector<vector<int>> adj(n);
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		while (k--)cin >> v, adj[u-1].push_back(v-1);
	}
	queue<pair<int, int>> q;//<idx,path>
	q.push(pair<int, int>(0, 1));
	while (!q.empty()) {
		auto tmp = q.front(); q.pop();
		ans[tmp.first] = min(ans[tmp.first], tmp.second);
		for (int i = 0; i < adj[tmp.first].size(); i++) q.push(pair<int, int>(adj[tmp.first][i], tmp.second+1));
	}
	for (int i = 0; i < n; i++)	cout << i + 1 << " " << (ans[i] == 1000000000 ? -1 : ans[i]-1) << endl;
	
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010


int main(){
  int n;
  bool connected[200][200] = {};
  
  scanf("%d", &n);

  for(int i=0;i<n;i++){
    int u, k, v;

    scanf("%d%d", &u, &k);
    u--;
    
    for(int j=0;j<k;j++){
      scanf("%d", &v);
      v--;

      connected[u][v] = true;
    }
  }

  queue<int> q1, q2;
  bool visited[200] = {};
  int ans[200];

  for(int i=0;i<n;i++) ans[i] = -1;
  
  q1.push(0);

  for(int i=0;q1.size();i++){

    while(q1.size()){
      int p = q1.front();
      q1.pop();

      if(visited[p]) continue;
      visited[p] = true;

      ans[p] = i;
      
      for(int j=0;j<n;j++){
        if(connected[p][j]){
          q2.push(j);
        }
      }
    }

    q1 = q2;
    q2 = queue<int>();
  }

  for(int i=0;i<n;i++)
    printf("%d\n", ans[i]);
  
  return 0;
}