Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: 
#include<iostream>
#include<queue>
using namespace std;
#define NIL -999
bool mark[105][105];
int n;

int d[105] = { 0 };

inline int next(int i) {
	for (int tmp = 1; tmp <= n; ++tmp) {
		if (mark[i][tmp] && d[tmp]==-1) {
			//cout << endl << i << "的下一个节点是：" << tmp << endl;
			return tmp;
		}
	}
	//cout << endl << i << "的下一个节点是：" << NIL << endl;
	return NIL;
}
void bfs_IN(int i) {
	int tm = 0;
	queue<int> q;
	int now, nt;
	int status = 1;;
	q.push(i);
	d[i] = tm;
	//cout << 3 << endl;
	++tm;
	while (!q.empty()) {
		now = q.front();
		nt = next(now);
		while (nt != NIL) {
			//status = true;
			q.push(nt);
			if (-1==d[nt])d[nt] = tm;
			nt = next(now);
		//	cout << 4 << endl;
		}
		q.pop();
		--status;
		if (!status){
			status = q.size();
			++tm;
		}
		//status = false;
	}
}
void bfs() {
	int k, Node, tmp;

	for (int i = 1; i <= n; ++i)
		for (int j = 0; j <= n; ++j)
			mark[i][j] = false;

	cin >> n;
	for (int i = 0; i <= n; ++i)
		d[i] = -1;
	for (int i = 1; i <= n; ++i) {
		cin >> Node >> k;
		for (int j = 1; j <= k; ++j) {
			cin >> tmp;
			mark[Node][tmp] = true;
		}
	}

	bfs_IN(1);

	//cout << 1 << endl;
	for (int p = 1; p <= n; ++p) {
		cout << p << ' ' << d[p] << endl;
	}

}



int main() {

	bfs();
	return 0;
}

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

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

int n,M[N][N];
int d[N];
void bfs(int s){
  int i,u,v;
  queue<int> q;
  q.push(s);
  for(i=0;i<n;i++)d[i]=INF;
  d[s]=0;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(v=0;v<n;v++){
      if(d[v]!=INF)continue;
      if(d[v]!=INF)continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }
  for(i=0;i<n;i++){
    cout << i+1 << " " << ((d[i]==INF) ? (-1) : d[i])<<endl;
  }
}
int main(){
  int u,k,v;
  int i,j;
  cin >> n;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++)M[i][j]=0;
  }
  for(i=0;i<n;i++){
    cin >> u >> k;
    u--;
    for(j=0;j<k;j++){
      cin >> v;
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);

  return 0;
}

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

#define VISITED 1

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> D(n, 0);
  vector<vector<int> > graph(n);
  vector<int> dist(n, -1);
  queue<int> q;
  for(int i = 0; i < n; i++) {
    int u, k, v_i;
    cin >> u >> k;
    for(int j = 0; j < k; j++) {
      cin >> v_i;
      graph[u-1].push_back(v_i-1);
    }
  }
  q.push(0);
  D[0] = VISITED;
  dist[0] = 0;
  while(!q.empty()) {
    int id = q.front();
    q.pop();
    for(int i = 0; i < graph[id].size(); i++) {
      if(D[graph[id][i]] != VISITED) {
	dist[graph[id][i]] = dist[id] + 1;
	q.push(graph[id][i]);
	D[graph[id][i]] = VISITED;
      }
    }
  }

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

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;

class Queue{
private:
  int inf[MAX];
  int head,tail;
 
public:
 
  Queue(){
    syokika();
  }
 
  void syokika(){
    head = 0;
    tail = 0;
  }
 
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
 
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
 
  int front(){
    return inf[head];
  }
 
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
 
  int size(){
    return tail - head;
  }
 
};

int imap[200][200];
int k[200] = {0}; 
int ans[200] = {0};
bool used[200];

int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }

   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j; 
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }

  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<k[u[i]];j++){
      cout << imap[u[i]][j] << " ";
    }
    cout << endl;
  }
  */

  Queue que = Queue();

  que.push(u[0]);

  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
	que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
	used[imap[p][i]] = true;
	ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;

 
  

 
  for(int i=0;i<n;i++){
    cout << u[i] << " " << ans[u[i]] << endl;
  }

  return 0;
}