Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

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

int G[110][110];
int N,num,lines,tar;
int D[1000];
int ret[1000];

void bfs(int src);

int main() {
	for (int i=0;i<110;i++) {
		for(int j=0;j<110;j++) {
			G[i][j] = 0;
		}
	}
	for(int i=0;i<1000;i++) {
		D[i] = 100;
	}
	cin >> N;
	for(int i=0;i<N;i++) {
		cin >> num >> lines;
		for(int j=0;j<lines;j++) {
			cin >> tar;
			G[num][tar] = 1;
		}
	}
	/*
	for(int i=0;i<num+1;i++) {
		for(int j=0;j<num+1;j++) {
			cout << G[i][j] << " ";
		}
		cout << endl;
	}
	*/
	bfs(1);
	for(int i=1;i<num+1;i++) {
		cout << i << " " << ret[i] << endl;
	}

}
void bfs(int src) {
	int cur,dst;
	//cerr << "bfs root = " << src << endl;
	queue<int> Q; // ??´??°????????????????????\????????????
	Q.push(src);
	D[src] = 0; // ?????????
	while (! Q.empty()) {
		int cur = Q.front(); // ??????????´????????????????
		Q.pop();
// ???????¢??????¨??¨???
		ret[cur] = D[cur];
		//cerr << cur << " " << D[cur] << endl;
		for (int dst=1;dst<N+1;dst++) { // ???????????? dst ????????????
			if ((G[cur][dst] == 1) && (D[dst] == 100)) { // cur ?????? dst ??????????????????dst ???????¨??????????
				D[dst] = D[cur]+1; //
				Q.push(dst); // dst ????¨???????????????????
			}
		}
	}
}

C++: #include <iostream>
using namespace std;
#define N 100
#define WHITE 0
#define BLACK 1

int M[N+1][N+1];
int n, dist;
int DIST[N+1];
int color[N+1];


//キューを使うプログラムを考える
void bfs(int r) {
    int u;
    int next[N+1], next_num = 0;
    bool added = false;
    //現時点での距離を入れておく
    for(int i=1; i<=n; i++) {
        if(M[r][i]==1 && color[i]==WHITE) {
            if(!added) {
                dist++;
                added = true;
            }
            color[i] = BLACK;
            DIST[i] = dist;
            next[next_num] = i;
            next_num++;
        }
    }
    
    for(int i=0; i<next_num; i++) {
        bfs(next[i]);
    }
}   

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

    cin >>n;
    for(int i=0; i<=n; i++) {
        DIST[i] = -1;
        color[i] = WHITE;
        for(int j=0; j<=n; j++) {
            M[i][j] = 0;
        }
    }

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

    dist = 0;
    DIST[1] = 0;
    bfs(1);

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

    return 0;
}

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

struct node {
	std::vector<int> child;
	int distance;
};

int main() {
	int node_num;

	std::cin >> node_num;

	std::vector<node> n(node_num);

	for (int i = 0; i < node_num; i++) {
		int num, to, node_val;
		std::cin >> node_val >> num;
		for (int j = 0; j < num; j++) {
			std::cin >> to;
			n[node_val - 1].child.push_back(to);
		}
		n[i].distance = -1;
	}

	//???????????????????????¢0
	n[0].distance = 0;
	std::queue<int> q;
	int dist = 1;
	q.push(1);
	while(q.size() != 0){
		int num = q.front();
		q.pop();
		for (std::vector<int>::iterator it = n[num - 1].child.begin(); it != n[num - 1].child.end(); it++) {
			if (n[*it - 1].distance > n[num - 1].distance || n[*it - 1].distance == -1) {
				n[*it - 1].distance = n[num - 1].distance;
				q.push(*it);
			}
		}
	}

	for (int i = 0; i < node_num; i++) {
		std::cout << i + 1 << " " << n[i].distance << std::endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>

//#include<bits/stdc++.h>



using namespace std;




int main() {
	
	int n;
	cin>>n;
	vector<vector<int>>t(n,vector<int>(0));
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
		for(int j=0;j<b;j++){
			int x;
			cin>>x;x--;t[i].push_back(x);
		}
	}
	queue<int>r;
	r.push(0);
	int g[105]={1000000};
	for(int i=0;i<n;i++){
		g[i]=10000000;
	}
	g[0]=0;
	while(r.size()){
		int v=r.front();
		for(int i=0;i<t[v].size();i++){
			r.push(t[v][i]);
			g[t[v][i]]=min(g[t[v][i]],g[v]+1);
		}
		r.pop();
		

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

	
	return 0;
}

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

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

int M[N][N] = {0}, d[N], n;

void bfs() {
  int u, v;
  queue<int> q;
  for (u = 0; u < n; u++) {
    d[u] = INFTY;
  }
  d[0] = 0;
  q.push(0);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (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 (u = 0; u < n; u++) {
    cout << u + 1 << ' ' << ((d[u] == INFTY) ? (-1) : d[u]) << endl;
  }
}

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

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

  bfs();

  return 0;
}

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

typedef struct{
  int A[1000];
  int ans;
}node;
node T[1000];
int Q[1000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    for(int i=1;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	}
	Q[tail++]=i;
      }
    }
    num=Q[head++];
  }
  
}

int main(){
  int num;
  int son;
  int z;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    for(int j=0;j<son;j++){
      cin>>z;
      T[num].A[z]=1;
      }
  }

  function(1);
  for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
  }

  for(int i=1;i<=n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }


  return 0;
}