Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

C++: #include <algorithm>
#include<iostream>
#include<vector>
#include<deque>
#include<queue>
#include<list>
#include<stack>
#include<map>
#include<set>
#include<string>
#include <sstream>
#include<bitset>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>

using namespace std;
const int INF = 0x7fffffff;

int gcd(int x,int y){
  int mi,ma;
  ma = max(x,y);
  mi = min(x,y);
  if(ma % mi == 0){
    return mi;
  }
  else{
    return gcd(mi,ma % mi);
  }
}

int g[101][101];
int dist[101];
int seen[101];

int main(){

  long ii,jj,kk;
  vector<int> a;

  memset(g,0,sizeof(g));
  int n;
  cin >> n;
  int v,vn,r;
  for(ii=0;ii<n;ii++){
    cin >> v;
    cin >> vn;
    for(jj=0;jj<vn;jj++){
      cin >> r;
      g[v][r] = 1;
    }
  }
  vector<int> todo;
  memset(seen,0,sizeof(seen));
  for(ii=0;ii<101;ii++){
    dist[ii] = -1;
  }

  dist[1] = 0;
  seen[1] = 1;
  todo.push_back(1);
  //int v;
  while(!todo.empty()){

    v = todo[0];
    todo.erase(todo.begin());
    
    //cout << "v:" << v <<endl;
    for(ii=1;ii<=n;ii++){
      if(g[v][ii] == 1 && seen[ii] == 0){
        dist[ii] = dist[v] + 1;
        seen[ii] = 1;
        todo.push_back(ii);
      }
    }
    
  }

  for(ii=1;ii<=n;ii++){
    cout << ii << " " << dist[ii] << endl;
  }
  
//  for(ii=1;ii<=n;ii++){
//    for(jj=1;jj<=n;jj++){
//      cout << g[ii][jj] ;
//      if(jj!=n){
//        cout << " ";
//      }
//    }
//    cout << endl;
//  }
  
  return 0;
}

C++: #include "stdafx.h"
#include <utility>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

int D[110]; // ?????¢????¨???¶
int v, A[100][100], u, k, w;
queue<int> Q;

void bfs(int src) {

	Q.push(src);
	D[src] = 0;

	while (!Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int i = 0; i < v; ++i) {
			if (A[cur][i] == 1 && D[i] == 0) {
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}

	for (int l = 0; l < v; ++l) {
		if (D[l] == 0 && l != 0) { D[l] = -1; }
		cout << l+1 << " " << D[l] << endl;
	}

}

int main(){
	cin >> v;
	// ???????????§??????v
	for (int i = 0; i < v; ++i) {

		cin >> u >> k;
		// ??????????????????u???k?????\?????????????????°??????

		for (int j = 0; j < k; ++j) {

			cin >> w;
			A[u - 1][w - 1] = 1;

			// ??£??\???????????§?¨??????????????????????????????????

		}
	}

	
	bfs(0);

}

C++: #include <iostream>
#include <stdio.h>
#include <queue>
#define N 101
#define INFTY (1<<21)


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

void bfs(int u) {
    std::queue<int> Q;
    Q.push(u);
    for (int i = 0; i < n; i++) d[i] = INFTY;
    d[u] = 0;
    while ( ! Q.empty() ) {
        int j = Q.front();
        Q.pop();
        for (int v = 0; v < n; v++) {
            if (M[j][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[j] + 1;
            Q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}

int main() {
    int u, k, v;
    scanf("%d", &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++) {
        scanf("%d %d", &u, &k);
        u--;
        if (k == 0) continue;
        for (int j = 0; j < k; j++) {
            scanf("%d", &v);
            v--;
            M[u][v] = 1;
        }
    }
    
    bfs(0);
    
    return 0;
}

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

typedef struct{
  int A[1000];
  int ans;
  bool c;
}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=2;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	}
	Q[tail++]=i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    num=Q[head++];
    //  cout<<head<<" "<<tail<<endl;
  }

}

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;
      }
  }
  Q[0]=1;
  function(1);
  T[1].ans=0;
  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;
}