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