Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include <climits>
#include <map>
#include <set>
const int mod = 1e9 + 7;
const long long INF = 1LL << 60;
using namespace std;
typedef long long ll;
 
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 <cstdio>
#include <queue>
using namespace std;

int dt[101][101];
int d[101];

int n;
int t=0;
void bfs(int s){
	priority_queue<int, vector<int>, greater<int> > que;
	for(int i=1;i<=n;i++)
		d[i]=-1;
	d[s]=0;
	que.push(s);
	while(!que.empty()){
		int nxt=que.top();que.pop();
		for(int i=1;i<=n;i++){
			if(dt[nxt][i]&&d[i]==-1){
				que.push(i);
				d[i]=d[nxt]+1;
			}
		}
	}
}
int main(){
	int fi, v, g;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d %d",&fi,&v);
		for(int j=1;j<=v;j++){
			scanf("%d",&g);
			dt[fi][g]=1;
		}
	}
	d[1]=0;
	bfs(1);
	for(int i=1;i<=n;i++){
		if(!d[i]){
			bfs(i);
		}
	}
			
	for(int i=1;i<=n;i++){
		printf("%d %d\n",i,d[i]);
	}
	return 0;
}

C++: #include <cstdio>
#include <queue>
using namespace std;
#define MAX_N 100

int g[MAX_N+1][MAX_N+1];
int d[MAX_N+1];
bool visited[MAX_N+1];
queue<int> que;

int n;
int t = 1;

void bfs(int id) {
  visited[id]=true;
  que.push(id);
  queue<int> next_que;
  while(!que.empty()) {
    int v = que.front(); que.pop();
    for(int u=1;u<=n;u++) {
      if(g[v][u]!=0 && !visited[u]) {
        visited[u]=true;
        next_que.push(u);
        d[u] = t;
      }
    }
    if(que.empty()){
      que = next_que;
      while(!next_que.empty()) next_que.pop();
      t++;
    }
  }
}

int main() {
  scanf("%d\n",&n);
  int u,k,v;
  for(int i=0;i<n;i++) {
    scanf("%d %d ",&u,&k);
    for(int j=0;j<k;j++) {
      scanf("%d ",&v);
      g[u][v] = 1;
    }
  }
  bfs(1);
  for(int i=1;i<=n;i++) {
    if(visited[i])
      printf("%d %d\n",i,d[i]);
    else
      printf("%d -1\n",i);
  }
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int N = 100;
static const int INFTY = (1 << 21);

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

void bfs(int s){
    for(int i = 0; i < n; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }

    queue<int> q;
    color[s] = GRAY;
    d[s] = 0;
    q.push(s);

    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int v = 0; v < n; v++){
            if(M[u][v] && color[v] == WHITE){
                color[v] = GRAY;
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
        color[u] = BLACK;
    }

    for(int i = 0; i < n; i++){
        if(d[i] != INFTY) cout << i + 1 << " " << d[i] << endl;
        else cout << i + 1 << " " << -1 << endl;
    }
}

int main(){
    cin >> n;
    int u, k, v;
    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;
        for(int j = 0; j < k; j++){
            cin >> v;
            M[u-1][v-1] = 1;
        }
    }
    bfs(0);

    return 0;
}

C++: // 2015/01/05 Tazoe

#include <iostream>
#include <queue>
using namespace std;

int main()
{
	int n;
	cin >> n;

	int G[100][100] = {0};

	for(int i=0; i<n; i++){
		int u, k;
		cin >> u >> k;

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

			G[u-1][v-1] = 1;
		}
	}

	int d[100];
	for(int i=0; i<n; i++){
		d[i] = -1;
	}

	queue<int> Q;

	Q.push(0);
	d[0] = 0;

	while(!Q.empty()){
		int u = Q.front();

		int v = -1;
		for(int j=0; j<n; j++){
			if(G[u][j]==1 && d[j]==-1){
				v = j;
				break;
			}
		}

		if(v!=-1){
			Q.push(v);
			d[v] = d[u]+1;
		}
		else{
			Q.pop();
		}
	}

	for(int i=0; i<n; i++){
		cout << i+1 << ' ' << 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 kansuu(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 i=0;i<n;i++){
      if(M[u][i]==0) continue;
      if(d[i]!=INFTY) continue;
      d[i]=d[u]+1;
      q.push(i);
    }
  }
  for(int i=0;i<n;i++){
    if(d[i]==INFTY) d[i]=-1;
    cout<<i+1<<" "<<d[i];
    printf("\n");
  }
}
int main(void){
  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;
    }
  }
  kansuu(0);
  return 0;
}