Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: /**
 * Bredth first search.
 * graphs could be circular.
 * there could be a better answer later.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N] = {0};
int s[N] = {0};

void bfs(int n, int dd) {
	int k = adj[n][0];
	int kk = 0;
	for (int j=1;j<=k;j++) {
		int nn = adj[n][j];
		if (d[nn] != -1 && d[nn] <= dd) continue; // this condition is important.
		d[nn]=dd;
		s[kk++]=nn;
	}

	for (int j=0;j<kk;j++)
	{
		bfs(s[j], dd+1);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		d[j]=-1;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
	}

	d[1]=0;

	bfs(1, 1);

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

	return 0;
}

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