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 <vector>
#include <queue>

using namespace std;

const int kSize = 101;

vector<int> adj[kSize];
bool isMarked[kSize];
int d[kSize];

void dfs(int v) {
    isMarked[v] = true;

    for (int i = 0; i < adj[v].size(); ++i) {
        int u = adj[v][i];
        if (!isMarked[u]) {
            dfs(u);
        }
    }
}

void bfs() {
    queue<int> q;
    q.push(1);
    d[1] = 0;
    int dis = 0;
    isMarked[1] = true;

    while (!q.empty()) {
        int size = q.size();
        for (int i = 0; i < size; ++i) {
            int u = q.front();
            q.pop();
            for (int j = 0; j < adj[u].size(); ++j) {
                int v = adj[u][j];
                if (!isMarked[v]) {
                    q.push(v);
                    isMarked[v] = true;
                }
            }

            d[u] = dis;
        }

        dis++;
    }
}

int main() {
    int n;
    fill(isMarked, isMarked + kSize, false);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int id, k;
        cin >> id >> k;
        for (int j = 0; j < k; ++j) {
            int v;
            cin >> v;
            adj[id].push_back(v);
        }
    }

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

    return 0;
}

C++: #include<cstdio>
#include<queue>
#define MAX 100
#define INF 1000
using namespace std;
int n, g[MAX][MAX], labels[MAX], d[MAX];
queue<int> Q;

void bfs(int s) {
    int v,u;
    labels[s] = 1;
    d[s]=0;
    Q.push(s);
    while (!Q.empty()) {
	u = Q.front(); Q.pop();
	for (v=0; v<n; v++) {
	    if (g[u][v] && labels[v]==0) {
		labels[v] =1;
		d[v] = d[u] +1;
		Q.push(v);
	    }
	}
	labels[u] = 2;
    }
}

int main() {
    int i,u,k,v,j;
    scanf("%d",&n);
    for(i=0;i<n;i++){
	for(j=0;j<n;j++){
	    g[i][j]=0;
	}
    }

    for(i=0;i<n;i++){
	scanf("%d %d",&u, &k);
	for(j=0;j<k;j++){
	    scanf("%d",&v);
	    g[u-1][v-1]=1;
	}
    }

    for(i=0; i<n; i++) { labels[i]=0; d[i]=INF;}

    bfs(0);

    for(i=0; i<n; i++) {
	if (d[i] != INF) printf("%d %d\n", i+1, d[i]);
	else printf("%d -1\n", i+1);
    }
    return 0;
}

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

void bfs(int n, int dd) {
	int s[N] = {0}; // this needs to be a stack member.
	int k = adj[n][0];
//	cout << n << ' ' << dd << ' ' << k << endl;
	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;
	//	cout << nn << endl;
	}

	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<vector>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
  queue<int> Q;
  int n,u,k,vv,cur,dst;
  cin >> n;
  vector<int> D(n,-1),check(n,0);
  vector< vector<int> > v(n);
  for(int i = 0;i < n;++i){
    cin >> u >> k;
    for(int j = 0;j < k;++j){
      cin >> vv;
      v[u - 1].push_back(vv);
    }
    sort(v[u - 1].begin(),v[u - 1].end());
  }
  D[0] = 0;
  check[0] = 1;
  Q.push(1);
  while(!Q.empty()){
    cur = Q.front();
    Q.pop();
    for(int i = 0;i < v[cur - 1].size();++i){
      dst = v[cur - 1][i];
      if(check[dst - 1] == 0){
	D[dst - 1] = D[cur - 1] + 1;
	check[dst - 1] = 1;
	Q.push(dst);
      }
    }
  }
  for(int i = 0;i < n;++i)
    cout << i + 1 << " " << D[i] << endl;
}

C++: #include <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
#include <list>
#define inf 200
using namespace std;

int n;
list<int> adj[101];
int d[101];
bool f[101];
void BFS(int v)
{
    int m = 0;
    f[v] = false;
    while (!adj[v].empty())
    {
        d[adj[v].front()] = min(d[v] + 1, d[adj[v].front()]);
        adj[v].pop_front();
    }
    for (int i = 1; i <= n; i++)
    {
        if (f[i] && d[i] < d[m])
            m = i;
    }
    if (m > 0)
        BFS(m);
}

void print()
{
    for (int i = 1; i <= n; i++)
    {
        if (d[i] == inf)
            d[i] = -1;
        printf("%d %d\n", i, d[i]);
    }
}

int main()
{
    int u, k, v;
    cin >> n;
    for (int i = 0; i <= n; i++)
    {
        d[i] = inf;
        f[i] = true;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> u >> k;
        while (k > 0)
        {
            cin >> v;
            adj[u].push_back(v);
            k--;
        }
    }
    d[1] = 0;
    BFS(1);

    print();

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int INF=(1<<21);
int n,M[101][101];
int d[101]; //最短距離を記録(到達不可な時はINF)
void bfs(int s){
  queue<int>q;
  q.push(s);//始点sをいれる(訪問)
  for(int i=0;i<n;i++) d[i]=INF;
  d[s]=0;
  while(!q.empty()){
    int u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==0) continue; //辺がない
      if(d[v]!=INF) continue; //訪問したことがある
      d[v]=d[u]+1; //vは未訪問の頂点であり、隣接しているためd[u]に1を足す
      q.push(v); //vを始点に
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF)?(-1):d[i] )<<endl;
  } //到達不可能か
}
int main(){
  int u,k,v;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d%d",&u,&k);
    u--;
    for(int j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);
  return 0;
}