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>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

vector<int> g[101];
bool used[101];
int ans[101];
void bfs(int sr){
    queue<pint> q;q.push({sr,0});
    used[sr]=true;
    while(!q.empty()){
        pint pi=q.front();q.pop();
        ans[pi.first]=pi.second;
        rep(i,g[pi.first].size()){
            int nx=g[pi.first][i];
            if(!used[nx]){
                used[nx]=true;
                q.push({nx,pi.second+1});
            }
        }
    }
}
int main(){
    int n,u,k,v;
    cin>>n;
    rep(i,n){
        cin>>u>>k;
        --u;
        rep(j,k){
            cin>>v;
            --v;
            g[u].pb(v);
        }
    }
    memset(ans,-1,sizeof(ans));
    rep(i,n){
        if(!used[i]) bfs(i);
    }
    rep(i,n) cout<<i+1<<" "<<ans[i]<<endl;
    return 0;
}

C++: #include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<ll> VLL;
typedef vector<PLL> VP;
const static int INF = 1000000000;
const static int MOD = 1000000007;
const int dx[4]={1, 0, -1, 0};
const int dy[4]={0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i,n) for (ll i=0; i<(ll)(n); i++)
#define repd(i,n) for (ll i=n-1; i>=0; i--)
#define rept(i,m,n) for(ll i=m; i<n; i++)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define PF push_front
#define PB push_back
#define SORT(V) sort((V).begin(), (V).end())
#define RVERSE(V) reverse((V).begin(), (V).end())
#define paired make_pair
#define PRINT(V) for(auto v : (V)) cout << v << " "
//charを整数に
int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; }
// 累積和 for (int i = 0; i < N; ++i) s[i+1] = s[i] + a[i]; 
void cum_sum(int N,vector<double> a, vector<double> &s){ for(int i=0; i<N; i++){ s[i+1]=s[i]+a[i];}}
//ユークリッドの控除法
ll gcd(ll a,ll b){
    if(b == 0) return a;
    return gcd(b,a%b);
}
//最小公倍数
ll lcm(ll a,ll b){
    ll g = gcd(a,b);
    return a / g * b; // Be careful not to overflow
}
//素数判定
bool is_prime(long long n) {
    if (n <= 1) return false;
    for (long long p = 2; p * p <= n; ++p) {
        if (n % p == 0) return false;
    }
    return true;
}
int getdigit(ll num){
    unsigned digit=0;
    while(num!=0){
        num /= 10;
        digit++;
    }
    return digit;
}
//空白文字も入力 getline(cin, S);
//桁数指定 setprecision

int main()
{
    ios::sync_with_stdio(0); 
    cin.tie(0); 
    cout.tie(0);
    int n;
    cin >> n;
    vector<vector<int>> G(n);
    rep(i,n){
        int a;
        cin >> a;
        a--;
        int b;
         cin >> b;
        rep(j,b){
            int c;
            cin >> c;
            c--;
            G[a].PB(c);
        }
    }

    vector<int> dist(n, -1);
    dist[0]=0;
    queue<int> que;
    que.push(0);

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

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

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

static const int INFTY = (1<<29);
int M[100][100];
int d[100];
int n;

void dfs(int s){
    int u;
    queue<int> q;

    q.push(s);
    for(int i=0;i<n;i++) d[i]=INFTY;
    d[s]=0;

    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(void){
    int u,k,v;

    cin >> n;
 
    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;
	}
    }

    dfs(0);

    return 0;
}

C++: //
//  main.cpp
//  BFS
//  Copyright ?????? 2017 1. All rights reserved.
//

#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;


int main() {
    int n,u,k,d;
    vector<vector<int>>A(100,vector<int>(100));
    for(int i=0;i<100;i++)
        for(int j=0;j<100;j++)
            A[i][j]=0;
    
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>u>>k;
        for(int j=0;j<k;j++){
            cin>>d;
            A[u-1][d-1]=1;
        }
    }//input
    
    int D[100];
    for(int i=0;i<100;i++) D[i]=100000000;
    int src=0;
    queue<int>Q;
    Q.push(src);
    D[src]=0;
        while(!Q.empty()){
            int cur=Q.front();
            Q.pop();
            for(int dst=0;dst<n;dst++)
            {
                if(A[cur][dst]==1&&D[dst]==100000000){
                    
                    D[dst]=min(D[cur]+1,D[dst]);
                    Q.push(dst);
                }
        }
    }
    
    for(int i=0;i<n;i++){
        
        cout<<i+1<<" "<<D[i]<<endl;
    }
    
    

    
    
    }