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 <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
	int check_id;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
	check_id = 0;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == check_id) return -1;
	else check_id = id;

	if (id == u) {
		check_id = 0;
		return 0;
	}

	for (int i = 0; i < k; i++) {
		if (id == next[i]->u) {
			check_id = 0;
			return 1;
		}
	}
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d_min, d + 1);
	}
	check_id = 0;
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>

using namespace std;

struct Info{
	Info(){m_node = m_distance = 0;}
	Info(int node,int distance){
		m_node = node;
		m_distance = distance;
	}
	int m_node,m_distance;
};

class InfoQueue{
public:
	InfoQueue(){
		info = new Info[10000];
		num_of_data = head = tail = 0;
	}
	bool isEmpty(){
		return num_of_data == 0;
	}
	void enqueue(Info data){
		info[tail] = data;
		tail = (tail+1)%10000;
		num_of_data++;
	}
	Info dequeue(){
		Info ret = info[head];
		head = (head+1)%10000;
		num_of_data--;
		return ret;
	}
private:
	Info* info;
	int head,tail,num_of_data;
};


int main(){

	int n,row,col,num;
	scanf("%d",&n);

	int table[n+1][n+1];
	int check_table[n+1];

	for(int i=1;i<=n;i++){
		check_table[i] = -1;
		for(int k=1;k<=n;k++)table[i][k] = 0;
	}

	for(int i=1;i<=n;i++){
		scanf("%d %d",&row,&num);
		for(int k=1;k<=num;k++){
			scanf("%d",&col);
			table[row][col]=1;
		}
	}

	check_table[1] = 0;

	InfoQueue Q;
	Q.enqueue(Info(1,0));

	Info tmp;
	while(!Q.isEmpty()){
		tmp = Q.dequeue();
		for(int i=1;i<=n;i++){
			if(table[tmp.m_node][i]==1 && check_table[i] == -1){
				check_table[i] = tmp.m_distance+1;
				Q.enqueue(Info(i,tmp.m_distance+1));
			}
		}
	}

	for(int i=1;i<=n;i++){
		printf("%d %d\n",i,check_table[i]);
	}

    return 0;
}

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

void BFS(vector<vector<int> >& V, vector<int>& time) {
	int i,k;
	int cnt=1;
	queue<pair<int,int> > qv;

	qv.push({1,0});
	time[1]=0;
	while(!qv.empty()) {
		k=qv.front().first;
		cnt=qv.front().second+1;
		qv.pop();
		for(i=0; i<(int)V[k].size(); ++i) {
			if(time[V[k][i]]==-1) {
				qv.push({V[k][i],cnt});
				time[V[k][i]]=cnt;
			}
		}
	}
}

int main() {
	int n,k,u;
	vector<vector<int> > V;
	vector<int> time;

	scanf("%d",&n);
	V.resize(n+1);
	time.resize(n+1);

	for(int i=1; i<=n; ++i) {
		scanf("%d%d",&u,&k);
		V[u].resize(k);
		time[u]=-1;
		for(int j=0; j<k; ++j) {
			scanf("%d",&V[u][j]);
		}
	}
	BFS(V,time);

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

C++: #include <iostream>
#include <queue>
#define INF 1000000
using namespace std;
class st
{
	public:
		int vertex;
		int dis;
	st();
	st(int,int);
};

st::st()
{
	vertex=-1;
	dis=-1;
}

st::st(int v,int d)
{
	vertex=v;
	dis=d;
}

int main()
{
	int n;
	cin>>n;
	int g[101][101];
	for(int i=0;i<=n;i++)
		for(int j=0;j<=n;j++)
		{
			if (i==j)
				g[i][j]=0;
			else
				g[i][j]=INF;
		}
	for(int i=0;i<n;i++)
	{
		int f,en;
		cin>>f>>en;
		for(int j=0;j<en;j++)
		{
			int t;
			cin>>t;
			g[f][t]=1;

		}
	}

/*	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			cout<<g[i][j]<<" ";
		cout<<endl;
	}
*/
	int visited[101];
	for(int i=0;i<=n;i++)
		visited[i]=INF;

	queue<st> Q;
	st first=st(1,0);
	Q.push(first);
	while(!Q.empty())
	{
		st now=Q.front();
		Q.pop();
		for(int i=1;i<=n;i++)
		{
			if(g[now.vertex][i]+now.dis<visited[i])
			{
				visited[i]=g[now.vertex][i]+now.dis;
				Q.push(st(i,visited[i]));
			}
		}
	}
	
	for(int i=1;i<=n;i++)
		cout<<i<<" "<<(visited[i]>=INF?-i:visited[i])<<endl;
	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int n ,dist[128];
bool rel[128][128];

int main() {
	scanf("%d", &n);
	
	for(int i = 1; i <= n; i++){
		int u, k;
		scanf("%d %d", &u, &k);
		
		dist[u] = INT_MAX;
		for(int j = 1; j <= n; j++){
			rel[u][j] = false;
		}
		while(k--){
			int v;
			scanf("%d", &v);
			rel[u][v] = true;
		}
	}
	
	queue<int> visit;
	visit.push(1);
	dist[1] = 0;
	
	while(visit.size()){
		for(int i = 1; i <= n; i++){
			if(rel[visit.front()][i] && 1 + dist[visit.front()] < dist[i]){
				visit.push(i);
				dist[i] = 1 + dist[visit.front()];
			}
		}
		visit.pop();
	}
	
	for(int i = 1; i <= n; i++){
		printf("%d %d\n", i, dist[i]==INT_MAX ? -1 : dist[i]);
	}
	return 0;
}

C++: def dfs(src)
  D[src] = $time
  (0..N - 1).each do |dst|
    if D[dst] == nil && G[src][dst] == 1
      D[dst] = D[src] + 1
      dfs(dst)
    end
  end
end

N = gets.to_i
G = (1..N).map{ Array.new(N, 0) }
N.times do
  u, k, *v = gets.split(' ').map(&:to_i)
  v.each do |vi|
    G[u - 1][vi - 1] = 1
  end
end

D = Array.new(N) #??????????¨????????????????
# F = Array.new(N) #?????£???????????????


# $time = 1
D[0] = 0

(0..N-1).each do |id|
  dfs(id) unless D[id]
end

(0..N-1).each do |id|
  puts "#{id + 1} #{D[id]}"
end