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 <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
mat g;

lli u,k,v;

queue<lli> q;
queue<lli> d;
vbl vis;
vll ans;


int main(){
    cin >> n;
    g = mat(n+1);
    vis = vbl(n+1);
    ans = vll(n+1);
    for(lli i = 0;i < n;i++){
        cin >> u >> k;
        for(lli j = 0;j < k;j++){
            cin >> v;
            g[u].push_back(v);
        }
    }
    q.push(1);
    d.push(0);
    while(!q.empty()){
        lli p = q.front();
        lli r = d.front();
        q.pop();
        d.pop();
        if(vis[p]) continue;
        vis[p] = true;
        ans[p] = r;
        for(lli i = 0;i < g[p].size();i++){
            if(!vis[g[p][i]]) q.push(g[p][i]),d.push(r+1);
        }
    }
    for(lli i = 1;i <= n;i++) cout << i << " " << ans[i] << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;

  vector<vector<int>> M(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    int node;
    int d;
    cin >> node >> d;
    node--;

    for (int j = 0; j < d; j++) {
      int tmp;
      cin >> tmp;
      tmp--;
      M[i][tmp] = 1;
    }
  }

  vector<int> res(n, -1);
  vector<int> e(n);
  res[0] = 0;

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    int num = q.front();
    q.pop();

    int cnt = res[num] + 1;

    for (int i = 0; i < M[num].size(); i++) {
      if (M[num][i] == 0) { continue; }
      if (e[i] == 1) { continue; }
      q.push(i);
      e[i]   = 1;
      res[i] = cnt;
    }
  }
  res[0] = 0;

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

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  rep(i,n){
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
       d[i][j]=100000;
      }
    }
  }
  rep(i,n){
    int u,k;
    cin >> u >> k;
    q1.push(u);
    if(k>0){
     int v[k];
     rep(j,k){
       cin >> v[j];
       q1.push(v[j]);
     }
     int f = q1.front()-1;
     q1.pop();
     rep(i,k){
       int e=q1.front()-1;
       d[f][e]=1;
       q1.pop();
     }
    }else{
     q1.pop();
    }
  }

  rep(i,n){
    for(j=2;j<n;j++){
    d[0][i]=min(d[0][i],d[0][j]+d[j][i]);
    }
  }
 
 rep(i,n){
   cout << i+1 << ' ' <<d[0][i] << endl;
 }
 
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
using Graph = vector<vector<int>>;

int main() {
    // 頂点数と辺数
    int N; cin >> N ;

    // グラフ入力受取 (ここでは無向グラフを想定)
    Graph G(N);
    for (int i = 0; i < N; ++i) {
        int a , b;
        cin >> a >> b;
        a = a - 1;
        if(b != 0){
        for(int j = 0; j < b; j++){
            int e1;
            cin >> e1;

            e1 = e1 - 1;
        //    G[e1].push_back(a);
            G[a].push_back(e1);
        }
        }

    }

    // BFS のためのデータ構造
    vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[0] = 0;
    que.push(0); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v]) {
            if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない
            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    // 結果出力 (各頂点の頂点 0 からの距離を見る)
    for (int v = 1; v <= N; ++v) cout << v << " " << dist[v - 1] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T>
void view(const std::vector<T> &v)
{
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion

int main()
{
    int n;
    cin >> n;
    vector<vector<int>> g(n, vector<int>());
    vector<int> d(n, -1);

    rep(i, n)
    {
        int k;
        cin >> k >> k;
        rep(j, k)
        {
            int t;
            cin >> t;
            t--;
            g[i].emplace_back(t);
        }
    }

    queue<int> q;
    q.push(0);
    d[0] = 0;
    while (q.size())
    {
        int v = q.front();
        q.pop();

        for (auto nv : g[v])
        {
            if (d[nv] >= 0)
                continue;

            d[nv] = d[v] + 1;
            q.push(nv);
        }
        // cout << v << " " << q.size() << endl;
    }

    rep(i, n)
    {
        cout << (i + 1) << " " << d[i] << endl;
    }

    return 0;
}

C++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=ja
*/

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(){
    int n;
    cin >> n;
    vector<vector<int>> to(n);
    rep(i,n){
        int u,k;
        cin >> u >> k;
        rep(j,k){
            int v;
            cin >> v;
            v--;
            to[i].push_back(v);
        }
    }

    vector<int> dist(n,-1);
    queue<int> q;
    dist[0]=0;
    q.push(0);

    while(!q.empty()){
        int p;
        p = q.front();
        q.pop();
        //cout << "p" << p << endl;
        rep(i,to[p].size()){
            if(dist[to[p][i]]==-1){
                dist[to[p][i]]=dist[p]+1;
                q.push(to[p][i]);
            }
        }
    }

    rep(i,dist.size()){
        cout << i+1 <<" " <<dist[i] << endl;
    }
}