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;
int graph[125][125];
int d[125];
void bfs(int s, int n){
	queue<int> Q;
	int color[125];	
	for(int i=1;i<=n;i++){
		color[i] =0;
		d[i] = 2e9;
	}
	color[s] = 1;
	d[s] = 0;
	Q.push(s);
	while(Q.size()!=0){
		int u = Q.front();
		Q.pop();
		for(int v=1;v<=n;v++){
			if(graph[u][v] == 0) continue;
			if(color[v] == 0){
				color[v] = 1;
				d[v] = d[u] +1;
				Q.push(v);
			}
		}
		color[u] = 2;
	}
}

int main(){
	int n;
	cin >> n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			graph[i][j] = 0;
		}
	}
	for(int i=1;i<=n;i++){
		int num;
		int k;
		int tmp;
		cin >> num >> k;
		if(k !=0){
			for(int j=0;j<k;j++){
				cin >> tmp;
				graph[num][tmp] = 1;
			}
		}
	}
	bfs(1, n);
	for(int i=1;i<=n;i++){
		cout << i << " "<<((d[i] == 2e9)? (-1) :d[i]) << endl;
	}
}

C++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

#define MAX 100
//#define MAX_ 1000
struct Graph
{
	int id, k;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
//int cou = 0;
void Breadth_cou(Graph *g,int cou)
{
	for (auto i = g->node.begin(); i != g->node.end(); ++i) {
		if (G[*i].d == -1 || G[*i].d > cou)
			G[*i].d = cou;	
	}
	g->k = 0;
}
void Breadth(Graph *g)
{
	if (g->k > 0) {
		Breadth_cou(g, g->d + 1);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			Breadth(&G[*i]);
		}
	}
}
int n;
int main(void)
{
	int u, xk, v;//, n;
	scanf_s("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf_s("%d %d", &u, &xk);
		G[u - 1].id = u - 1;
		G[u - 1].k = xk;
		for (int j = 0; j < xk; ++j) {
			scanf_s("%d", &v);
			G[u - 1].node.push_back(v - 1);
		}
	}
	G[0].d = 0;
	Breadth(&G[0]);
	for (int i = 0; i < n; ++i) {
		printf("%d %d\n", G[i].id + 1, G[i].d);
	}
}

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

#define NODE_NUM 100

queue<int> que;

class Node
{
private:
	int next_num;
	int next_node_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id);
	bool Search(int id);
	int dist;
	int check[NODE_NUM];
};

Node::Node()
{
	next_num = 0;
	dist = -1;
	for (int i = 1; i <= NODE_NUM; i++) {
		check[i] = 0;
	}
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node Node_list[NODE_NUM + 1];

bool Node::Search(int id)
{
	for (int k = 0; k < next_num; k++) {
		if (Node_list[next_node_list[k]].dist == -1) {
			Node_list[next_node_list[k]].dist = dist + 1;
		}
	}

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == id) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		if (Node_list[next_node_list[j]].check[id] == 0) {
			Node_list[next_node_list[j]].check[id] = 1;
			que.push(next_node_list[j]);
		}
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u;
		cin >> k;
		Node_list[u] = Node();
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			Node_list[u].AddNextNode(v);
		}
	}
	
	//dist
	Node_list[1].dist = 0;
	for (int i = 2; i <= n; i++) {
		bool ret = Node_list[1].Search(i);
		while (!ret && !que.empty()) {
			ret = Node_list[que.front()].Search(i);
			que.pop();
		};
		while (!que.empty()) {
			que.pop();
		}
	}

	//output
	for (int i = 1; i <= n; i++) {
		cout << i << " " << Node_list[i].dist << "\n";
	}
	
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <memory>
#include <string>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define MAX 105

struct Vtx{
    int id;
    int path;
    Vtx(){path=id=-1;}
    bool operator <(Vtx &b){
        return id<b.id;
    }
};

Vtx V[MAX];

set<int> adj[MAX];

int n,u,k,v;

void bfs(){
     queue<int> q;
     q.push(1);
     V[1].path=0;
     set<int> e;
     set<int>::iterator it;
     while(!q.empty()){
        e = adj[q.front()];
        it = e.begin();
        while(it!=e.end()){
            if(V[*it].path==-1){
                V[*it].path = V[q.front()].path+1;
                q.push(*it);
            }
            it++;
        }
        q.pop();
     }

}

void print(){
    for(int i=1;i<=n;i++){
        printf("%d %d\n",i,V[i].path);
    }
}


void solve(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&u,&k);
        V[u].id=i;
        for(int j=0;j<k;j++){
            scanf("%d",&v);
            adj[u].insert(v);
        }
    }
    bfs();
    print();
}

int main(){

   // freopen("in","r",stdin);
    solve();
    return 0;
}

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  int m[n];
  rep(i,n){
    m[i]=10000;
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
        d[i][j]=10000;
      }
    }
  }
  m[0]=0;

  rep(i,n){
    int u,k;
    cin >> u >> k;
    if(k>0){
      rep(j,k){
        int v;
        cin >> v;
        if(d[v-1][u-1]!=1){
          d[u-1][v-1]=1;
        }
      }
    }
  }   
  q1.push(0);
  rep(i,n){
    int u = q1.front();
    rep(j,n){
      if(d[u][j]==1){
        q1.push(j);
        m[j] = min(m[j],m[u]+1);
      }
    }
    q1.pop();
  }

  rep(i,n){
    cout << i+1 << ' ';
    if(m[i]==10000){
      cout << -1 << endl;
    }else{
      cout << m[i] << endl;
    }
  }

  return 0;
}