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 <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG true
// sort(a.begin(), a.end(), std::greater<int>());

/*
std::vector<std::string> split(const std::string &input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter))
    {
        result.push_back(field);
    }
    return result;
}
*/
vector<int> data[100];
int finds[100];
/*
int dfs(int foo, int timer)
{
    //foo is node number
    if (finds[foo].first != -1)
        return timer;
    finds[foo].first = ++timer;
    int result = timer;
    if (data[foo].size() != 0)
    {
        rep(i, data[foo].size())
        {
            result = dfs(data[foo][i], result);
        }
    }
    finds[foo].second = ++result;
    return result;
}
*/
int main()
{
    int n;
    cin >> n;
    rep(i, n)
    {
        int temp;
        cin >> temp;
        cin >> temp;
        rep(j, temp)
        {
            int hoge;
            cin >> hoge;
            data[i].push_back(hoge - 1);
        }
        finds[i] = -1;
    }
    queue<int> que;
    que.push(0);
    finds[0] = 0;
    while (!que.empty())
    {
        int temp = que.front();
        que.pop();
        rep(i, data[temp].size())
        {
            if (finds[data[temp][i]] == -1)
            {
                finds[data[temp][i]] = finds[temp] + 1;
                que.push(data[temp][i]);
            }
        }
    }
    rep(i, n)
    {
        cout << i + 1 << " " << finds[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 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() {

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

  int u, k, v;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--; // to 0 origin
    for (int j = 0; j < n; j++) {
      cin >> v;
      v--; // to 0 origin
      M[u][v] = 1;
    }
  }

  bfs(0);

  return 0;
}

C++: #include <bits/stdc++.h>
#include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int a[101][101],start[101],end[101],n,p=0,v[101];
queue<int> q;
void bfs(int u){
	q.push(u);
	v[u]++;
	while(!q.empty()){
		u=q.front();q.pop();
		r(i,1,n+1)if(!v[i]&&a[u][i]){
			v[i]++;
			q.push(i);
			start[i]=start[u]+1;
		}
	}
}
int main(){
	int q,qq,qqq;
	cin>>n;
	r(i,0,n){
		cin>>q>>qq;
		r(i,0,qq){
			cin>>qqq;
			a[q][qqq]++;
		}
	}
	//r(i,1,n+1)if(!v[i])
	bfs(i);
	r(i,1,n+1)cout<<i<<' '<<start[i]<<endl;
}

C++: #include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
static const int MAX = 100;
int N;
int ADJ[MAX][MAX];
int depth[MAX];

enum STATUS { ACTIVE = 0, MARKED, VISITED, };
int status[MAX] = {ACTIVE};

void visit(int id) {
  status[id] = VISITED;
}
void mark(int id) {
  status[id] = MARKED;
}

void bfs(int id) {
  queue<int> q;
  q.push(id);
  depth[id] = 0;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    visit(u);
    for (int i = 0; i < N; i++) { 
      if (ADJ[u][i] && status[i] == ACTIVE) {
        q.push(i);
        mark(i);
        depth[i] = depth[u] + 1;
      }
    }
  }
}

int main() {
  cin >> N;
  int id, nn, x;
  for (int i = 0; i < N; i++) {
    cin >> id >> nn;
    id--; // 0-based
    for (int j = 0; j < nn; j++) {
      cin >> x;
      x--; // 0-based
      ADJ[id][x] = 1;
    }
  }
  for (int i = 0; i < N; i++)
    depth[i] = -1;

  bfs(0);
  for (int i = 0; i < N; i++)
    cout << i+1 << " " << depth[i] << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cstdio>
#include<queue>
 
using namespace std;
 
int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離
 
void bfs(int src){
    queue<int> Q;
    Q.push(src);
    fill(D, D + N, -1);
    D[src] = 0;
    while (! Q.empty()){
        int cur = Q.front();
        Q.pop();
        cerr << cur+1 << " " << D[cur] << endl;
        for (int k = 0; k < N; k++){
            if (G[cur][k] == 1 && D[k] == -1){
                D[k] = D[cur]+1;
                Q.push(k);
            }
        }
    }
}
 
int main(){
    scanf("%d", &N);
        int id, c;
        for (int i = 0; i < N; i++){
            scanf("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
            scanf("%d", V + id);
            for (int j = 0; j < V[id]; j++){
                scanf("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
                G[id][c] = 1;
            }
        }//データの読み込み完了。
    bfs(0);
   return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> d;
vector<int> f;
vector<bool> used;
vector< vector<int> > v;
int n;
int cnt = 0;


int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  d = f = vector<int>(n, -1);
  used = vector<bool>(n, false);
  v = vector< vector<int> >(n, vector<int>(n, 0));
  for(int i = 0; i < n; i++){
    int u, k; cin >> u >> k;
    u--;
    for(int j = 0; j < k; j++){
      int a; cin >> a;
      a--;
      v[u][a] = 1;
    }
  }

  queue<int> que;
  que.push(0);
  d[0] = cnt;
  used[0] = true;
  while(!que.empty()){
    cnt++;
    int q = que.front(); que.pop();
    for(int i = 0; i < n; i++){
      if(v[q][i] && !used[i]){
        used[i] = true;
        que.push(i);
        d[i] = d[q]+1;
      }
    }
  }

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

  return 0;
}