Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

}