Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

func (q *Queue) push(v Pair) {
	*q = append(*q, v)
}

func (q *Queue) pop() Pair {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;

ll d[110]; // 距離
ll w[110][110];
queue<int> qu;
ll White = 0, Gray = 1, Black = 2;
ll color[110];

void solve() {
    Rep(i, 1, 100) d[i] = -1;

    ll n; cin >> n;
    Rep(i, 1, n){
        ll u, k;
        cin >> u >> k;
        Rep(j, 1, k){
            ll v; cin >> v;
            w[i][v] = 1;
        };
    }

    qu.push(1);
    d[1] = 0;
    while(!qu.empty()){
        int u = qu.front(); qu.pop();
        Rep(v, 1, n){
            if(w[u][v] == 1 && color[v] == 0){
                d[v] = d[u] + 1; 
                qu.push(v);
                color[v] = Black;
            }
        }
        color[u] = Black;
    }

    Rep(i, 1, n) cout << i << " " << d[i] << endl;

    return;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    solve();
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main(void)
{
    int n;
    cin >> n;
    vector<vector<int> > G(n+1);
    int distv[n + 1];
    rep(i, n+1)
    {
        distv[i] = -1;
    }

    rep(i, n)
    {
        int num, deg;
        cin >> num >> deg;
        rep(j, deg)
        {
            int v;
            cin >> v;
            G[num].push_back(v);
        }
    }

    distv[1] = 0;
    queue<int> q;
    q.push(1);

    while(!q.empty())
    {
        int v = q.front();
        q.pop();
        for(int nv : G[v])
        {
            if(distv[nv] != -1) continue;
            q.push(nv);
            distv[nv] = distv[v] + 1;
        }
    }

    rep(i, n)
    {
        cout << i+1 << " " << distv[i+1] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stdio.h>
#define REP(i, n) for(int i = 0;i < n;i++)

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);
	REP(i, n)
		d[i] = INFTY;
	d[s] = 0;
	int u;

	while (!q.empty)
	{
		u = q.front();
		q.pop();
		REP(v, n)
		{
			if (M[u][v] == 0)
				continue;
			if (d[v] != INFTY)
				continue;
			d[v] = d[u]++;
			q.push(v);
		}
	}
	REP(i, n)
		cout << i++ << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
}

int main()
{
	int u, k, v;

	cin >> n;

	REP(i, n)
	{
		REP(j, n)
			M[n][n] = 0;
	}

	REP(i, n)
	{
		cin >> u >> k;
		u--;
		REP(i, k)
		{
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}

	bfs(0);
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#include <vector> 
using namespace std;
 
vector<int> G[100];
int d[100];

int main(){
    int n;
    cin >> n;
    for(int i=0; i<n; i++) d[i] = -1;

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

    queue<int> q;
    q.push(0);
    d[0] = 0;
    while(!q.empty()){
        for(int &v : G[q.front()]){
            if(d[v] == -1){
                q.push(v);
                d[v] = d[q.front()] + 1;
            }
        }
        q.pop();
    }

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

    return 0;
}

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

const int INF = -1;
int n;
int a[100][100], d[100];

int dist(int id) {
	int t = 0;
	queue<int> pd;

	for (int i = 0; i < n; i++)
		d[i] = INF;

	pd.push(1);
	d[0] = 0;

	while (pd.size()) {
		int id1 = pd.front();
		pd.pop();

		if (id1 == id)
			break;

		for (int i = 0; i < n; i++) {
			if (a[id1 - 1][i] && d[i] == INF) {
				pd.push(i + 1);
				d[i] = d[id1 - 1] + 1;
			}
		}
	}
	return d[id - 1];
}

int main()
{
	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;
			a[u - 1][v - 1] = 1;
		}
	}

	for (int i = 1; i < n + 1; i++) {
		int dmin = dist(i);
		cout << i << " " << dmin << endl;
	}

	return 0;
}

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

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

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

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

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(u, 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 <iostream>
#include <queue>
using namespace std;

static const int N=100;
static const int INF = (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]=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;
        u--;
        for(int j=0;j<k;j++){
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}