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>

#define NIL -1
#define INF 1000000001

using namespace std;
void bfs(const vector<vector<int> > &adj, vector<int> &d, queue<int> node_visit){
    int current_node;
    while(!node_visit.empty()){
        current_node = node_visit.front();
        node_visit.pop();
        for(int i = 1;i < d.size();i++){
            if(adj[current_node][i] == 1 && d[i] == -1){
                d[i] = d[current_node]+1;
                node_visit.push(i);
            }
        }
    }
    
}
int main(){
    int num_node;
    int node_ind,node_deg,adj_node;
    scanf("%d",&num_node);

    vector<vector<int> > adj(num_node+1,vector<int>(num_node+1,0));
    queue<int> node_visit;
    vector<int> d(num_node+1,NIL);

    for(int i = 0; i < num_node;i++ ){
        scanf("%d %d",&node_ind,&node_deg);
        for(int j = 0;j < node_deg;j++){
            scanf("%d",&adj_node);
            adj[node_ind][adj_node] = 1;
        }
    }
   
    node_visit.push(1);
    d[1] = 0;
    bfs(adj,d,node_visit);
    for(int i = 1; i < num_node+1;i++){
        printf("%d %d\n",i,d[i]);
    }

    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]; 
/* d[N] are distances which describe the state whether the node is already visited.
   if the value == INFTY, the edge is NOT yet visited.
 */

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, 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--; // because the index is 0-origin!
        for (int j = 0; j < k; j++) {
            cin >> v;
            v--; // because the index is 0-origin!
            M[u][v] = 1; // edges between u and v are available (connected).
        }
    }
    
    bfs(0);
    
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
static const int MAX=101;

vector<int> Adjacency[MAX];
int dt[MAX];
bool Visited[MAX];

void bfs(int i)
{
    queue<int> Q;
    dt[i]=0;Q.push(i);
    Visited[i]=true;
    int d=0;
    while(!Q.empty())
    {
        int p=Q.front();Q.pop();d=dt[p]+1; //important
        for(int j=0;j<Adjacency[p].size();j++)
        {
            if(Visited[Adjacency[p][j]]==true) continue;
            //else
            Q.push(Adjacency[p][j]); Visited[Adjacency[p][j]]=true;
            dt[Adjacency[p][j]]=d;
        }
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int j,num;
        cin>>j>>num;
        for(int k=0;k<num;k++)
        {
            int t;cin>>t;
            Adjacency[j].push_back(t);
        }
    }
    bfs(1);
    for(int i=1;i<=n;i++)
    {
        cout<<i<<' '<<dt[i]<<endl;
    }
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
//const ll mod = 998244353;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<endl;}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<endl;};

string num2bit(ll num, ll len){
    string bit = "";
    REP(i, len){
    bit += char('0'+(num>>i & 1));
    }
    return bit;
}

vector<int> graph_bfs(Graph &G, vector<ll> &s){
    const int N = G.size();
    vector<int> dist(N, -1);
    queue<int> qu;

    // initialization
    for(auto v: s){
        qu.push(v);
        dist[v] = 0;
    }

    while(!qu.empty()){
        int now = qu.front();qu.pop();
        for(auto next: G[now]){
            if(dist[next] != -1) continue;
            dist[next] = dist[now] + 1;
            qu.push(next);
        }
    }

    return dist;
}

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

    ll n;
    cin >> n;
    Graph G(n);
    REP(i, n){
        ll u, k;
        cin >> u >> k;
        u--;
        REP(j, k){
            ll v;
            cin >> v;
            v--;
            G[u].push_back(v);
        }
    }

    vector<ll> s = {0};

    auto dist = graph_bfs(G, s);

    REP(i, n){
        cout << i+1 << " " << dist[i] << endl;
    }

    return 0;
}