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 <queue>
#include <algorithm>
using namespace std;

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

void set()
{
	int u, k, v;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			M[u][v] = 1;
		}
	}

	for (int i = 1; i <= n; i++)
		d[i] = -1;
}

void bfs(int x)
{
	queue<int> Q;

	Q.push(x);
	d[x] = 0;

	while (!Q.empty()) {
		int u = Q.front();
		for (int i = 1; i <= n; i++) {
			if (M[u][i] == 1 && d[i] == -1) {
				Q.push(i);
				d[i] = d[u] + 1;	
			}
		}
		Q.pop();
	}
}

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

	cin >> n;
	set();

	bfs(1);

	for (int i = 1; i <= n; i++)
		cout << i << " " << d[i] << endl;

	return 0;
}

C++: #include <iostream>
#include <queue>

static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
int d[N];

void bfs(int s)
{
    std::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++)
    {
        std::cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << std::endl;
    }
}

int main() 
{
    int u, k, v ;
    std::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++)
    {
        std::cin >> u >> k ;
        u--;
        for (int j = 0; j < k; j++)
        {
            std::cin >> v ;
            v-- ;
            M[u][v] = 1 ;
        }
        
    }
    
    bfs(0) ;
    return 0 ;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxx = 110;
int n, m[maxx][maxx];
int u, num, v;
int flag[maxx];
int d[maxx];


void bfs() {
	queue<int>q;

	q.push(1);
	d[1] = 0;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = 2; i <= n; i++) {
			if (m[u][i] == 1 && flag[i] == 0) {
				// ?????°???????????????
				flag[i] = 1;
				// ??´??°??????
				d[i] = d[u] + 1;
				// ?????\??????
				q.push(i);
			}
		}
	}
}
int main()
{

	scanf("%d", &n);
	memset(m, 0, sizeof(m));
	for (int i = 1; i <= n; i++) {
		scanf("%d %d", &u, &num);

		// ????§????
		flag[i] = 0;
		d[i] = -1;
		for (int j = 1; j <= num; j++) {
			scanf("%d", &v);
			m[u][v] = 1;
		}
	}
	bfs();
	for (int i = 1; i <= n; i++) {
		printf("%d %d\n", i, d[i]);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int N = 101;
const int CRL = -1;
int adj[N][N] = {0};
int d[N]={0};
int kai[N];

int main()
{
    int n;
    queue <int> q;
    q.push(1);
    cin >> n;
    for(int i=1;i<=n;i++){
    	int k;
    	int l;
    	cin >> l >> k;
    	kai[i]=l;
    	for(int j=1;j<k+1;j++){
    		int m;
    		cin >> m;
    		adj[i][j] = m;
    	}
    }
    while(q.size()){
    	int k = q.front();
    	q.pop();
    	for(int i=1;i<kai[i]+1;i++){
    		if(d[adj[k][i]]==0){
    			d[adj[k][i]] = d[k]+1;
    			q.push(adj[k][i]);
    		}
    	}
    }
    for(int i=1;i<=n;i++){
    	cout << i << " " << d[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,v,k;
    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<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;


void bfs(v2& a, v2& b){
	queue<int> q;
	q.push(a[0][0]);
	while(!q.empty()){
		int x = q.front()-1;
		int t = b[x][1];
		q.pop();
		rep(i, a[x][1]){
			int y = a[x][2+i]-1;
			if(a[y][0] != -1 && a[y][0] != -2){
				b[y][1] = t+1;
				a[y][0] = -1;
			}
			if(a[y][0] != -2) q.push(y+1);
		}
		a[x][0] = -2;
	}
}

int main(){
	int n;
	cin >> n;
	v2 a(n, v1(0));
	v2 b(n, v1(2, 0));
	rep(i, n){
		int m;
		cin >>m;
		a[i].push_back(m);
		b[i][0] = m;
		int x;
		cin >> x;
		a.at(i).push_back(x);
		rep(j, x){
			int y;
			cin >> y;
			a.at(i).push_back(y);
		}
	}
	bfs(a, b);
	for(int i = 1;i<n; i++){
		if(b[i][1] == 0) b[i][1] = -1;
	}
	rep(i, n){
		rep(j, 2){
			cout << b[i][j];
			if(j != 1) cout << " ";
			else cout << endl;
		}
	}
}