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;

typedef long long ll;


int d[200];
bool table[200][200];

void init(){
    for(int i=0;i<200;i++){
        d[i]=-1;
        for(int j=0;j<200;j++){
            table[i][j]=false;
        }
    }
}

void bfs(int itr, int dis, int n){
    d[itr] = dis;
    for(int i=0;i<n;i++){
        if(table[itr][i]&&(d[i]<0||d[i]>dis+1)){
            bfs(i, dis+1,n);
        }
    }
}

void solve(int n){
    bfs(0,0,n);
}

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

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

    solve(n);

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

    return 0;
}

C++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>

long long int INF = 1e18;
using namespace std;

typedef pair<long long int, long long int> P;

long long int DP[200][200] = {};

int n;

int main(){
	
	cin >> n;
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			DP[i][j] = INF;
		}
		DP[i][i] = 0;
	}
	for(int loop = 0; loop < n; loop++){
		
		int id, k;
		cin >> id >> k;
		id--;
		
		for(int i = 0; i < k; i++){
			int num;
			cin >> num;
			num--;
			DP[id][num] = 1;
		}
	}
	
	for(int k = 0; k < n; k++){
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				DP[i][j] = min(DP[i][j], DP[i][k] + DP[k][j]);
			}
		}
	}
	
	for(int i = 0; i < n; i++){
		cout << i + 1 << " ";
		if(DP[0][i] == INF){
			cout << -1 << endl;
		}else{
			cout << DP[0][i] << endl;
		}
	}
	
	return 0;
}

C++: #if 0
?????¨?????????2h
bfs??????????????¢?´¢???????????°????????§?????????
????????????????´????????????\?????????????????????????????¨????????????????????£??????????????????queue?????????????????¢?????????
main?????§???????????§?§£????????£??\?????????to??£??\??????????????????????????£??????
????????????????????????????????????????????¨?????´?????????
#endif

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

int n, D[100];
int G[100][100];
void bfs(int src){
	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(G[cur][dst]==1&&D[dst]==-1){
				D[dst]=D[cur]+1;
				q.push(dst);
			}
		}
	}
};

int main()
{
	vector<int> v[110];
	cin>>n;
	for(int i=0;i<n;i++){
		int u,k;
		cin>>u>>k;
		for(int j=0;j<k;j++){
			int tmp;
			cin>>tmp;
			v[u].push_back(tmp);
		}
	}
	for(int i=0;i<100;i++){
		D[i]=-1;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			G[i][j]=0;
		}
	}
	for(int i=1;i<n+1;i++){
		for(int j=0;j<v[i].size();j++){
			G[i-1][v[i][j]-1]=1; //
		}
	}
	bfs(0);
	for(int i=0;i<n;i++){
		cout<<(i+1)<<' '<<D[i]<<endl;
	}
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100;

vector<int> G[MAX_N];
int d[MAX_N];

int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		int id;
		cin >> id;
		int k;
		cin >> k;
		for(int j=0;j<k;j++){
			int to;
			cin >> to;
			G[id-1].push_back(to-1);
		}
	}
	fill(d,d+n,INF);
	deque<int> bfs;
	bfs.push_back(0);
	d[0] = 0;
	while(!bfs.empty()){
		int v = bfs.front();
		bfs.pop_front();
		for(int i=0;i<G[v].size();i++){
			int to = G[v][i];
			if(d[to] > d[v]+1){
				d[to] = d[v]+1;
				bfs.push_back(to);
			}
		}	
	}
	for(int i=0;i<n;i++){
		cout << i+1 << " " << d[i] << "\n";
	}
}

C++: #include <iostream>
//#include <iomanip>
//#include <string>
#include <vector>
#include <algorithm>
//#include <utility>
//#include <set>
//#include <map>
#include <queue>
//#include <deque>
//#include <bitset>
//#include <math.h>
using namespace std ;
using ll = long long ;
//using ld = long double ;
using vll = vector<ll> ;
using vvll = vector<vll> ;
//using vc = vector<char> ;
//using vvc = vector<vc> ;
//using vb = vector<bool> ;
//using vvb = vector<vb> ;
//using pll = pair<ll,ll> ;
//#define all(v) v.begin(),v.end()
//ll mod = 1000000007 ;
//long double pie = acos(-1) ;
ll INF = 1000000000000 ;

//void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
//ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
//ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
//ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
//void fix_cout(){cout << fixed << setprecision(20) ;}
//void chmin(ll &a,ll b){if(a>b) a = b ;}

int main(){
	ll n ; cin >> n ;
	vvll g(n,vll(0)) ;
	for(int i=0;i<n;i++){
		ll a,b ;
		cin >> a >> b ;
		for(int j=0;j<b;j++){
			ll c ; cin >> c ;
			c-- ;
			g.at(i).emplace_back(c) ;
		}
	}
	vll d(n,INF) ;
	d.at(0) = 0 ;
	queue<ll> q ;
	q.push(0) ;
	while(!q.empty()){
		ll p = q.front() ;
		q.pop() ;
		for(int i=0;i<g.at(p).size();i++){
			if(d.at(g.at(p).at(i))>d.at(p)+1){
				d.at(g.at(p).at(i)) = d.at(p)+1 ;
				q.push(g.at(p).at(i)) ;
			}
		}
	}
	for(int i=0;i<n;i++) cout << i+1 << " " << (d.at(i)==INF?-1:d.at(i)) << endl ;
}

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

using namespace std;

int D[100100];//始点からの距離を入れる
int g[1001][1001]={{0}};//要素を0で初期化
void bfs(int src,int n){//srcは開始位置この場合だと頂点1だから0
  cerr << "bfs_root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()  ){
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting_" << cur << " " <<D[cur] <<endl;
    for(int dst= 0; dst < n; dst++){
      if(g[cur][dst]==1 && D[dst]==0){
        D[dst]=D[cur]+1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n){
  for (int i = 0; i < n; ++i)
  {
    cout << i+1 <<D[i]<<endl;
  }

}

int main(){
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n,a,b,c;
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> a >> b ;
    for (int i = 0; i < b; ++i)
    {
      cin >> c;
      g[a-1][c-1]=1;/* code */
    }
  }
  bfs(0,n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }


}