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;
typedef long int li;
typedef long double lb;
#define rep(i,j,n) for (ll i = j; i < (n); i++)
#define repr(i,j,n) for(ll i = j; i >= (n); i--)
#define all(x) (x).begin(),(x).end()
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<b)
#define out(ans) cout << ans << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef pair<ll,ll>P;
const ll mod=1e9+7;
const ll INF = 1LL<<60;

int main(){

  ll n;
  cin>>n;
  ll ans[n];
  vector<vector<ll> >V;
  V.resize(n);
  rep(i,0,n){
    ll a;cin>>a;
    --a;
    ll k;cin>>k;
    rep(j,0,k){
      ll b;cin>>b;
      --b;
      V[a].push_back(b);
    }
    ans[i]=INF;
  }

  queue<ll>q;
  q.push(0);

  ans[0]=0;
  while(!q.empty()){
    ll u=q.front();q.pop();

    for(auto v:V[u]){
      // out(u<<":"<<v);
      if(ans[v]!=INF)continue;
      chmin(ans[v],ans[u]+1);
      q.push(v);
    }

  }


  rep(i,0,n){
    // out(ans[i]<<"=="<<inf);
    if(ans[i]==INF)out(i+1<<" "<<-1);
    else out(i+1<<" "<<ans[i]);
  }


  return 0;
}

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

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

int n, G[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(G[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++) G[i][j] = 0;
  }

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

  bfs(0);
  return 0;
}

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

int g[101][101], d[101];
deque<pair<int,int> > q;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0; i<n; i++) {
        int id, k;
        cin>>id>>k;
        g[id][0]=k;
        for(int j=1; j<=k; j++) {
            cin>>g[id][j];
        }
    }
    for(int i=0; i<=100; i++) d[i]=-1;
    q.push_back(make_pair(1,0));
    
    while(!q.empty()){
        pair<int, int> now = q.front();
        q.pop_front();
        int id=now.first, cost=now.second;
        if(d[id]!=-1) continue;
        d[id]=cost++;
        int vn=g[id][0];
        for(int i=1; i<=vn; i++) q.push_back(make_pair(g[id][i],cost));
    }
    for(int i=1; i<=n; i++) cout<<i<<' '<<d[i]<<'\n';
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++)
#define Sum(vec) accumulate(vec.begin(), vec.end(), 0)
#define Sort(vec) sort(vec.begin(), vec.end())
#define Reverse(vec) reverse(vec.begin(), vec.end())
#define Next_permutation(vec) next_permutation(vec.begin(), vec.end())

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, 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;
}

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

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)
using namespace std;

int main(){
    int n,u,k,v,src=0;
    cin >> n;
    int D[n],g[n][n];
    REP(i,n){
        D[i] = -1;
        REP(j,n){
            g[i][j] = 0;
        }    
    }
    REP(i,n){ 
        cin >> u >> k;
            REP(j,k){
                cin >> v;
                g[v-1][u-1] = 1;
            }
    }

    queue<int> Q;
    Q.push(src);
    D[src] = 0;
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
            REP(i,n){
                if(g[i][cur] == 1 && D[i] < 0){
                    D[i] = D[cur] + 1;
                    Q.push(i);
                }
            }
    }

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

C++: #include <stdio.h>
#include <limits.h>
#include <queue>

#define N 100

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

static void bfs(int s)
{
    std::queue<int> q;
    q.push(s);

    d[s] = 0;

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v = 0; v < n; v++) {
            if (M[u][v] == 0)
                continue;
            if (d[v] != INT_MAX)
                continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }

    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i + 1, (d[i] == INT_MAX) ? -1 : d[i]);
    }
}

int main()
{
    int u, k;
    
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            M[i][j] = 0;
        }
        d[i] = INT_MAX;
    }
    
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        for (int j = 0; j < k; j++) {
            int v;
            scanf("%d", &v);
            M[u - 1][v - 1] = 1;
        }
    }
    
    bfs(0);
    return 0;
}