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 <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
#include <utility>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i, s, n) for(int i=(s); i<=(n); ++i)
#define outl(x) cout<< (x) << '\n'
#define fi first
#define se second
#define mkp make_pair

#define INF 0x3f3f3f3f
using namespace std;

int N;
vector <int> G[102];
int far[102];

void slv()
{
   queue <int> que;
   que.push(1);
   memset( far, INF, sizeof(far) );
   far[1] = 0;

   while(!que.empty()){
      const int u = que.front();
      que.pop();
      rep(i, G[u].size()){
         const int next = G[u][i];
         if(far[u] + 1 < far[next]){
            far[next] = far[u] + 1;
            que.push( next );
         }
      }
   }

   reps(i, 1, N){
      cout << i <<  ' ' << ( far[i] == INF ? -1 : far[i] ) << '\n';
   }
   return;
}

signed main()
{
   cin.tie(0), ios::sync_with_stdio(false);
   cin >> N;

   reps(i, 1, N){
      int u, k, a;
      cin >> u >> k;
      while(k--){
         cin >> a;
         G[u].push_back(a);
      }
   }

   slv();
   return (0);
}

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

// 総数を1000000007で割った余り
const long long mod = 1e9 + 7;

using ll  = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
// #define SORT(v, n) stable_sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

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

int n, M[N][N];
int d[N];  // 距離で訪問状態(color)を管理する。

void bfs(int s) {
    queue<int> q;  //標準ライブラリのqueueを使用。
    q.push(s);
    rep(i, n) d[i] = INFTY;
    d[s]           = 0;
    int u;
    while (!q.empty()) {
        u = q.front();
        q.pop();
        rep(v, n) {
            if (M[u][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    rep(i, n) {
        cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // cout << fixed << setprecision(5);

    int u, k, v;
    cin >> n;
    rep(i, n) { rep(j, n) M[i][j] = 0; }
    rep(i, n) {
        cin >> u >> k;
        u--;
        rep(j, k) {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0);

    return 0;
}

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

int z[100],n,w[100];
int a[100][100];

void bfs(){
  int m;
  z[0]=0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    m=q.front();
    q.pop();
    if(w[m]==1) continue;
    w[m]=1;
    for(int i=0;i<n;i++){
      if(a[m][i]==1){
	q.push(i);
	z[i]=min(z[i],z[m]+1);
      }
    }
  }
}


int main(){
  int p,x;
  for(int i=0;i<100;i++){
    z[i]=w[i]=INF;
    for(int j=0;j<100;j++) a[i][j]=0;
  }
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>p>>p;
    for(int j=0;j<p;j++){
      cin>>x;
      a[i][x-1]=1;
    }
  }
  bfs();
  for(int i=0;i<n;i++){
    if(z[i]!=INF) cout<<i+1<<" "<<z[i]<<endl;
    else cout<<i+1<<" "<<-1<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
  queue<int> Q;
  int n,u,k,vv,cur,dst;
  cin >> n;
  vector<int> D(n,-1);
  vector< vector<int> > v(n);
  for(int i = 0;i < n;++i){
    cin >> u >> k;
    for(int j = 0;j < k;++j){
      cin >> vv;
      v[u - 1].push_back(vv);
    }
    sort(v[u - 1].begin(),v[u - 1].end());
  }
  D[0] = 0;
  Q.push(1);
  while(!Q.empty()){
    cur = Q.front();
    Q.pop();
    for(int i = 0;i < v[cur - 1].size();++i){
      dst = v[cur - 1][i];
      if(D[dst - 1] == -1) D[dst - 1] = D[cur - 1] + 1;
      Q.push(dst);
    }
  }
  for(int i = 0;i < n;++i)
    cout << i + 1 << " " << D[i] << endl;
}

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,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 <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 100000

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