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 <stdio.h>
#include <stdlib.h>

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_N 110

int n,time,head,tail;
int queue[MAX_N];
int *dist;
int *color;
int **matrix;

void enqueue(int);
int dequeue(void);
void bfs(void);

int main(){
  int i,j,u,k,v;

  scanf("%d",&n);

  //queue = malloc(n * sizeof(int));
  color = malloc(n * sizeof(int));
  dist = malloc(n * sizeof(int));

  matrix = malloc(n * sizeof(int *));
  for(i = 0 ; i < n ; i++){
    matrix[i] = malloc(n * sizeof(int));
 }
 for(i = 0 ; i < n ; i++){
   dist[i] = -1;
 }
 for(i = 0 ; i < n ; i++){
   for(j = 0 ; j < n ; j++){
     matrix[i][j] = 0;
   }
 }

 for(i = 0 ; i < n ; i++){
    scanf("%d",&u); u--;
    scanf("%d",&k);
    for(j = 0 ; j < k ; j++){
      scanf("%d",&v); v--;
      matrix[u][v] = 1;
    }
  }

  bfs();
  for(i = 0 ; i < n ; i++){
    printf("%d %d\n",i+1,dist[i]);
  }

  for(i = 0 ; i < n ; i++){
    free(matrix[i]);
  }
  free(matrix);
  //free(queue);
  free(dist);
  free(color);
  return 0;
}

void bfs(){
  int u,i;

  head = tail = 0;
  color[0] = GRAY;
  dist[0] = 0;
  enqueue(0);

  while(head != tail){
    u = dequeue();
    for(i = 0 ; i < n ; i++){
      if(matrix[u][i] == 1 && color[i] == WHITE){
        color[i] = GRAY;
        dist[i] = dist[u] + 1;
        enqueue(i);
      }
    }
    color[u] = BLACK;
  }
}

void enqueue(int q){
  queue[tail%MAX_N] = q;
  tail++;
}

int dequeue(){
  int v = queue[head%MAX_N];
  head++;
  return v;
}

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

using namespace std;

	int TIME = 0;
	int d[101];
	int num;
	int graph[101][101] = {0};

int main(){
	int hon;
	int eda;
	int ver;
	queue<int> QUE;

	cin>>num;

	for(int i=1;i<=num;i++)
		d[i] = -1;

	for(int i=0;i<num;i++){
		cin>>eda;
		cin>>hon;
		for(int j=0;j<hon;j++){
			cin>>ver;
			graph[eda][ver]++;
		}
	}
	QUE.push(1); //start
	d[1] = 0;

	do{
		ver = QUE.front();
		QUE.pop();
		for(int i=1;i<=num;i++){
			if(graph[ver][i] == 1 && d[i]==-1){
				d[i] = d[ver] + 1;
				QUE.push(i);
			}
		}
	}while(!QUE.empty());
		
	for(int i=1;i<=num;i++){
		cout<<i<<" "<<d[i]<<endl;
	}

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

void bfs(int);

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;

}

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

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

using namespace std; 

int main(void) {
	int nov,noe,u,v;
	cin >> nov;
	vector< vector<int> > adj(nov);
	for (int i = 0; i < nov; ++i) {
		cin >> u;
		cin >> noe;
		adj[u-1] = vector<int>(noe);
		for (int j = 0; j < noe; ++j) {
			cin >> adj[u-1][j];
			--adj[u-1][j];
		}
	}
	vector<int> res(nov,-1);
	res[0] = 0;
	queue< pair<int,int> > q;
	q.push({0,0});
	pair<int,int> now;
	while (!q.empty()) {
		now = q.front();
		q.pop();
		for (int i = 0; i < adj[now.first].size(); ++i) {
			if (res[adj[now.first][i]] < 0) {
				res[adj[now.first][i]] = now.second+1;
				q.push({adj[now.first][i],now.second+1});
			}
		}
	}
	for (int i = 0; i < nov; ++i) {
		cout << i+1 << " ";
		cout << res[i] << endl;
	}
	return 0;
}

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

int main(){
	int n,m,l,i,j,p;
	int f[1000][1000];
	int D[1000] = {};
	cin >> n;
	for(i = 0; i < n; i++){
		cin >> m;
		cin >> p;
		for(j = 0; j < p; j++){
			cin >> l;
			f[m][l] = 1;
		}
	}
	queue<int> Q;
	Q.push(1);
	while(!Q.empty()){
		int c = Q.front();
		Q.pop();
		for(i = 2; i <= n; i++){
			if(f[c][i] == 1 && D[i] == 0){
				D[i] = D[c] + 1;
				Q.push(i);
			}
		}
	}				
	for(i = 1; i <= n; i++){
		if (D[i] == 0 && i !=1)
			D[i] = -1;
		cout << i << " " << D[i] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define fi first
#define se second
#define mp make_pair
#define itrfor(itr,A) for(auto itr = A.begin(); itr !=A.end();itr++)
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
typedef long long llong;
char moji[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char moji2[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char moji3[10]={'0','1','2','3','4','5','6','7','8','9'};
#define Sort(a) sort(a.begin(),a.end());
#define Reverse(a) reverse(a.begin(),a.end());
#define print(a) cout << a << endl;
#define MOD llong(1e9+7)
#define MAX int(2 * 1e5 +5)
#define debug(x)  cout << #x << " = " << (x) << endl;
#define pi acos(-1.0)
#define int llong
#define INF llong(1e18 + 3)
template<class T> bool chmax(T &a,  T b) {if(a<b){a=b; return 1;} return 0;}
template<class T> bool chmin(T &a,  T b) {if(a>b){a=b; return 1;} return 0;}
bool Add(int &a,int b){a = (a + b) % MOD;}
void myprint(int* A,int A_num){
   REP(i,A_num) cout << A[i] << " ";
   cout << endl;
}



signed main(){
    int n;
    cin >> n;
    int dist[n];
    vector<int> G[n];
    REP(i,n) dist[i] = -1;
    REP(i,n){
        int v,k;
        cin >> v >> k;
        REP(j,k){
            int to;
            cin >> to;
            to --;
            G[i].push_back(to);
        }
    }

    queue<int> Q;
    dist[0] = 0;
    Q.push(0);
    while(Q.size()){
        int v = Q.front();
        Q.pop();
        itrfor(itr,G[v]){
            if(dist[*itr] ==  -1){
                Q.push(*itr);
                dist[*itr] = dist[v] + 1;
            }
        }
    }
    REP(i,n){
        cout << i + 1 << " " << dist[i] << endl;
    }

}