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 <set>
#include <vector>
#include <queue>
#include <stack>
#include <utility>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int g[101][101],n,d[101];

int main() {
    for(int i = 0;i < 101;i++)d[i] = -1;
    cin >> n;
    for(int i = 0;i < n;i++) {
        int u,k,v;
        cin >> u >> k;
        for(int j = 0;j < k;j++) {
            cin >> v;
            g[u][v]++;
        }
    }
    queue<int> q;
    q.push(1);
    d[1] = 0;
    while(!q.empty()) {
        int u = q.front();q.pop();
        for(int i = 1;i <= n;i++) {
            if(g[u][i] && d[i] == -1)
            {
                d[i] = d[u]+1;
                q.push(i);
            }
        }
    }
    for(int i = 1;i <=n;i++) {
        cout << i << " " << d[i] <<endl;
    }
}

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

int main()
{
    int n;
    cin >> n;
    vector<vector<int>> g(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].push_back(v);
        }
    }
    vector<int> dist(n, -1);
    queue<int> q;
    q.push(0);
    dist[0] = 0;
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (auto next : g[v])
        {
            if (dist[next] != -1)
            {
                continue;
            }
            dist[next] = dist[v] + 1;
            q.push(next);
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << dist[i] << endl;
    }
}

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<bits/stdc++.h>
using namespace std;

#define MAX 100
#define NIL -1

int n, G_list[MAX+1];
int G[MAX+1][MAX+1];
int d[MAX+1][2];

void make_graph(void){
  scanf("%d", &n);

  for(int i=0; i<n; i++){
    int cnt, num, m;

    scanf("%d %d", &num, &cnt);
    G_list[i]=num;

    for(int j=0; j<cnt; j++){
      scanf("%d", &m);
      G[num][m]=1;
    }
  }
}

void check_depth(void){
  queue<int> Q;
  Q.push(G_list[0]);
  d[G_list[0]][0]=0;
  d[G_list[0]][1]=NIL;
  
  while(!Q.empty()){
    int top=Q.front();
    Q.pop();
    
    for(int j=0; j<n; j++){
      if(G[top][G_list[j]] == 1 && d[G_list[j]][1] != NIL){
	d[G_list[j]][0]=d[top][0]+1;
	d[G_list[j]][1]=NIL;
	Q.push(G_list[j]);
      }
    }
  }
}

void print(void){

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

int main(void){

  make_graph();
  check_depth();
  print();
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define P pair<int,int>
#define PP pair<P,int>
#define fi first
#define se second
#define INF 1e9
#define MOD 1000000007
#define REP(i,n) for(int i=0;i<n;i++)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define all(x) (x).begin(),(x).end()
int main(){
    int n;
    cin>>n;
    vector<vector<int> > g(n);
    REP(i,n){
        int a,b;
        cin>>a>>b;
        a--;
        REP(i,b){
            int t;
            cin>>t;
            t--;
            g[a].pb(t);
        }
    }
    vector<int> d(n,INF);
    queue<P> que;
    que.push(P(0,0));
    while(que.size()){
        P p=que.front();que.pop();
        if(p.se>=d[p.fi])continue;
        d[p.fi]=p.se;
        REP(i,g[p.fi].size()){
            que.push(P(g[p.fi][i],p.se+1));
        }
    }
    REP(i,n){
        cout<<i+1<<" ";
        if(d[i]==INF){
            cout<<-1<<endl;
        }else{
            cout<<d[i]<<endl;
        }
    }
    return 0;
}